Supports Fastify versions 5.x
npm warn While resolving: [email protected] npm warn Found: [email protected] npm warn node_modules/fastify npm warn fastify@"^5.0.0" from the root project npm warn npm warn Could not resolve dependency: npm warn peer fastify@"4.x.x" from [email protected] npm warn node_modules/fastify-socket.io npm warn fastify-socket.io@"^5.1.0" from the root project npm warn npm warn Conflicting peer dependency: [email protected] npm warn node_modules/fastify npm warn peer fastify@"4.x.x" from [email protected] npm warn node_modules/fastify-socket.io npm warn fastify-socket.io@"^5.1.0" from the root project
https://github.com/ducktors/fastify-socket.io/issues/177 There is already a pull request open pending approval. https://github.com/ducktors/fastify-socket.io/pull/186
Hello, please approve support for Fastify versions 5.x.
it has been 6 month...
please give it some time
@HassanKrayem in the meanwhile, published the fork, just npm install @ericedouard/fastify-socket.io
@HassanKrayem in the meanwhile, published the fork, just
npm install @ericedouard/fastify-socket.io
I tried but it doesn't recognize the import command. Here's another forked one: npm install fastify-socket.
Fastify Socket
@eric-edouard I have invited you to as maintainer of this repository. Unfortunately, the person who was directly following this repo is no longer very active.
Is there's a chance for an update in the foreseeable future?
As of this time, the fastify-socket.io library hasn't been updated for fastify v5. If you choose to use it regardless, you run into the issue of the io namespace not available on the fastify instance.
The easy fix is to follow what was recommended by the library itself: https://github.com/ducktors/fastify-socket.io?tab=readme-ov-file#typescript.
I'm including a copy-paste solution you can use in your project right-away:
import fastifyPlugin from "fastify-plugin";
import { Server, type ServerOptions } from "socket.io";
// Extend Fastify's TypeScript types
declare module "fastify" {
interface FastifyInstance {
socketIO: Server;
}
}
// Define plugin options
export type SocketIOOptions = Partial<ServerOptions> & {
preClose?: (done: Function) => void;
};
// Define plugin
const socketIO = fastifyPlugin<SocketIOOptions>(
async function (fastify, options) {
// Create Socket.IO instance attached to Fastify's native HTTP server
const socketIO = new Server(fastify.server, options);
// Decorate Fastify instance so you can use fastify.socketIO
fastify.decorate("socketIO", socketIO);
// Handle server pre-close (disconnect clients before shutdown)
fastify.addHook("preClose", (done) => {
if (options.preClose) {
return options.preClose(done);
}
fastify.socketIO.local.disconnectSockets(true);
done();
});
// Handle full close (close Socket.IO server)
fastify.addHook("onClose", (instance, done) => {
instance.socketIO.close();
done();
});
},
{ name: "socket-io" }
);
export default socketIO;
I hope this helps.
Link to https://github.com/ducktors/fastify-socket.io/issues/311
Link to https://github.com/ducktors/fastify-socket.io/issues/72