From 3d669cdfe382d3be447cdea11128d972d2ff70df Mon Sep 17 00:00:00 2001 From: Rodrigo <49363242+rodrigov9@users.noreply.github.com> Date: Tue, 21 Apr 2026 13:37:39 +0100 Subject: [PATCH 1/2] remove unmantained fastify plugin --- .../02-Server/server-initialization.md | 68 +++++++------------ 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/docs/categories/02-Server/server-initialization.md b/docs/categories/02-Server/server-initialization.md index e79fafd5..f9a9d87d 100644 --- a/docs/categories/02-Server/server-initialization.md +++ b/docs/categories/02-Server/server-initialization.md @@ -513,83 +513,63 @@ NestJS v7 and below relies on Socket.IO v2, while NestJS v8 relies on Socket.IO ### With Fastify -You need to register the [`fastify-socket.io`](https://github.com/alemagio/fastify-socket.io) plugin: - ```js -const fastify = require("fastify"); -const fastifyIO = require("fastify-socket.io"); - -const server = fastify(); -server.register(fastifyIO); +const { Server } = require("socket.io"); -server.get("/", (req, reply) => { - server.io.emit("hello"); -}); +const fastify = require('fastify')(); +const io = new Server(fastify.server, { /* options */ }); -server.ready().then(() => { - // we need to wait for the server to be ready, else `server.io` is undefined - server.io.on("connection", (socket) => { - // ... - }); +io.on("connection", (socket) => { + // ... }); -server.listen({ port: 3000 }); +fastify.listen({ port: 3000 }); ``` ```js -import fastify from "fastify"; -import fastifyIO from "fastify-socket.io"; - -const server = fastify(); -server.register(fastifyIO); +import Fastify from 'fastify'; +import { Server } from "socket.io"; +const { Server } = require("socket.io"); -server.get("/", (req, reply) => { - server.io.emit("hello"); -}); +const fastify = Fastify(); +const io = new Server(fastify.server, { /* options */ }); -server.ready().then(() => { - // we need to wait for the server to be ready, else `server.io` is undefined - server.io.on("connection", (socket) => { - // ... - }); +io.on("connection", (socket) => { + // ... }); -server.listen({ port: 3000 }); +fastify.listen({ port: 3000 }); ``` ```ts -import fastify from "fastify"; -import fastifyIO from "fastify-socket.io"; +import Fastify from 'fastify'; +import { Server } from "socket.io"; +const { Server } = require("socket.io"); -const server = fastify(); -server.register(fastifyIO); +const fastify = Fastify(); +const io = new Server(fastify.server, { /* options */ }); -server.get("/", (req, reply) => { - server.io.emit("hello"); -}); - -server.ready().then(() => { - // we need to wait for the server to be ready, else `server.io` is undefined - server.io.on("connection", (socket) => { - // ... - }); +io.on("connection", (socket) => { + // ... }); -server.listen({ port: 3000 }); +fastify.listen({ port: 3000 }); ``` +More information [here](https://fastify.dev/). + ### With µWebSockets.js {#with-uwebsocketsjs} ```js From 4233c6bcaf42f944c5fd6d2de640691a59642802 Mon Sep 17 00:00:00 2001 From: Rodrigo <49363242+rodrigov9@users.noreply.github.com> Date: Tue, 21 Apr 2026 18:02:26 +0100 Subject: [PATCH 2/2] add server closing to the server initialization with fastify guide --- .../02-Server/server-initialization.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/categories/02-Server/server-initialization.md b/docs/categories/02-Server/server-initialization.md index f9a9d87d..31430920 100644 --- a/docs/categories/02-Server/server-initialization.md +++ b/docs/categories/02-Server/server-initialization.md @@ -526,6 +526,15 @@ io.on("connection", (socket) => { // ... }); +app.addHook('preClose', done => { + io.local.disconnectSockets(true); + done(); +}); + +app.addHook('onClose', (_instance, done) => { + io.close(done); +}); + fastify.listen({ port: 3000 }); ``` @@ -544,6 +553,15 @@ io.on("connection", (socket) => { // ... }); +app.addHook('preClose', done => { + io.local.disconnectSockets(true); + done(); +}); + +app.addHook('onClose', (_instance, done) => { + io.close(done); +}); + fastify.listen({ port: 3000 }); ``` @@ -562,6 +580,15 @@ io.on("connection", (socket) => { // ... }); +app.addHook('preClose', done => { + io.local.disconnectSockets(true); + done(); +}); + +app.addHook('onClose', (_instance, done) => { + io.close(done); +}); + fastify.listen({ port: 3000 }); ```