Adapter
When scaling to multiple Socket.IO servers, you will need to replace the default in-memory adapter by another implementation, so the events are properly routed to all clients.
Besides the in-memory adapter, there are four official implementations:
- the Redis adapter
- the
- the Postgres adapter
There are also several other options which are maintained by the (awesome!) community:
- (e.g. RabbitMQ)
- NATS
You can have access to the adapter instance with:
Starting with , each Adapter instance emits the following events:
create-room
(argument: room)delete-room
(argument: room)join-room
(argument: room, id)- (argument: room, id)
Example:
This may be useful for example in a microservice setup, where all clients connect to the microservice M1, while the microservice M2 uses the emitter to broadcast packets (uni-directional communication).
The emitter also supports the utility methods that were added in socket.io@4.0.0
:
socketsJoin()
socketsLeave()
disconnectSockets()
// make all Socket instances disconnect
emitter.disconnectSockets();
// make all Socket instances in the "room1" room disconnect (and discard the low-level connection)
emitter.in("room1").disconnectSockets(true);
// make all Socket instances in the "room1" room of the "admin" namespace disconnect
emitter.of("/admin").in("room1").disconnectSockets();
// this also works with a single socket ID
emitter.of("/admin").in(theSocketId).disconnectSockets();