Skip to content

OverSamu/fastify-socket.io

 
 

Repository files navigation

fastify-socket.io

CI Test npm Coverage Status Maintainability OpenSSF Scorecard

fastify-socket.io enables the use of Socket.io in a Fastify application.

Supports Fastify versions 4.x Supports socket.io version 4.x

Install

npm i fastify-socket.io socket.io

Usage

Require fastify-socket.io and register it as any other plugin, it will add a io decorator.

const fastify = require("fastify")();

fastify.register(require("fastify-socket.io"), {
  // put your options here
});

fastify.get("/", (req, reply) => {
  fastify.io.emit("hello");
});

fastify.listen({ port: 3000 });

For more details see examples

You can use it as is without passing any option, or you can configure it as explained by Socket.io doc.

Hooks

By default the plugin will add a preClose hook that disconnects all the local sockets in order to close correctly the fastify server. In order to change this behaviour you can use preClose option:

await fastify.register(require('fastify-socket.io'), {
  preClose: (done) => {
    // do other things
    fastify.io.local.disconnectSockets(true);
    done();
  }
})

The plugin also adds an onClose hook which closes the socket server when the fastify instance is closed.

Typescript

The io decorator is NOT typed.

This is necessary to allow, eventually, the developer to be able to define custom types and make use of the socket.io new types system (doc).

For more info see the example

Acknowledgements

The code is a port for Fastify of socket.io.

License

Licensed under MIT.
socket.io license

About

Fastify plugin for Socket.io

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 96.8%
  • JavaScript 1.7%
  • Shell 1.5%