diff --git a/docs/categories/02-Server/server-initialization.md b/docs/categories/02-Server/server-initialization.md index e79fafd5..31430920 100644 --- a/docs/categories/02-Server/server-initialization.md +++ b/docs/categories/02-Server/server-initialization.md @@ -513,83 +513,90 @@ 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 } = require("socket.io"); -const server = fastify(); -server.register(fastifyIO); +const fastify = require('fastify')(); +const io = new Server(fastify.server, { /* options */ }); -server.get("/", (req, reply) => { - server.io.emit("hello"); +io.on("connection", (socket) => { + // ... }); -server.ready().then(() => { - // we need to wait for the server to be ready, else `server.io` is undefined - server.io.on("connection", (socket) => { - // ... - }); +app.addHook('preClose', done => { + io.local.disconnectSockets(true); + done(); }); -server.listen({ port: 3000 }); +app.addHook('onClose', (_instance, done) => { + io.close(done); +}); + +fastify.listen({ port: 3000 }); ``` ```js -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 */ }); + +io.on("connection", (socket) => { + // ... +}); -server.get("/", (req, reply) => { - server.io.emit("hello"); +app.addHook('preClose', done => { + io.local.disconnectSockets(true); + done(); }); -server.ready().then(() => { - // we need to wait for the server to be ready, else `server.io` is undefined - server.io.on("connection", (socket) => { - // ... - }); +app.addHook('onClose', (_instance, done) => { + io.close(done); }); -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"); +io.on("connection", (socket) => { + // ... }); -server.ready().then(() => { - // we need to wait for the server to be ready, else `server.io` is undefined - server.io.on("connection", (socket) => { - // ... - }); +app.addHook('preClose', done => { + io.local.disconnectSockets(true); + done(); }); -server.listen({ port: 3000 }); +app.addHook('onClose', (_instance, done) => { + io.close(done); +}); + +fastify.listen({ port: 3000 }); ``` +More information [here](https://fastify.dev/). + ### With µWebSockets.js {#with-uwebsocketsjs} ```js