fastify-socket.io icon indicating copy to clipboard operation
fastify-socket.io copied to clipboard

Supports Fastify versions 5.x

Open developer-sushant opened this issue 1 year ago • 9 comments

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

developer-sushant avatar Oct 01 '24 07:10 developer-sushant

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

mattkrins avatar Oct 12 '24 07:10 mattkrins

Hello, please approve support for Fastify versions 5.x.

it has been 6 month...

please give it some time

HassanKrayem avatar Apr 02 '25 15:04 HassanKrayem

@HassanKrayem in the meanwhile, published the fork, just npm install @ericedouard/fastify-socket.io

eric-edouard avatar Apr 26 '25 05:04 eric-edouard

@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

yubarajshrestha avatar Jun 07 '25 02:06 yubarajshrestha

@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.

matteovivona avatar Jun 25 '25 12:06 matteovivona

Is there's a chance for an update in the foreseeable future?

bangetto avatar Aug 27 '25 10:08 bangetto

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.

udohjeremiah avatar Oct 21 '25 12:10 udohjeremiah

Link to https://github.com/ducktors/fastify-socket.io/issues/311

matteovivona avatar Nov 19 '25 08:11 matteovivona

Link to https://github.com/ducktors/fastify-socket.io/issues/72

matteovivona avatar Nov 19 '25 08:11 matteovivona