Description
Hey there,
we are currently facing multiple issues with fetchSockets()
in a Nest.js backend using @socket.io/redis-adapter
.
First, we tried to use server.to("room").fetchSockets().then((socket) => ...)
to emit to all sockets across all containers (with the help of Redis adapter) as we needed to process every socket individually (emitting user-specific data to each socket).
We are running on AWS ElastiCache and noticed that when using the aforementioned mechanism, only sockets of the local container are returned, thus nobody who is connected to other containers will receive the event. This works fine locally across multiple containers though.
We came up with a work around that uses server.local.to("room").fetchSockets()
and then emits a server-side event in order to do the same on all other containers. We also found this to scale better as we're distributing business logic that runs on those sockets to all containers. So while we were pretty happy with the work around, it doesn't work unfortunately. And this time it is locally reproducible. For some reason, server.local.to("room").fetchSockets()
does (inconsistently) return all sockets across all containers, thus emitting the event to every socket multiple times.
When subscribing to Redis Pub/Sub using redis-cli
it also looks like every container is requesting all sockets from all containers, although using server.local....
.
We are pretty lost currently when it comes to ideas where to debug and how to fix this issue, therefore we're asking here. We also asked on the Nest.js discord, although we believe this is either a problem with the Redis adapter or with socket.io itself (thus my next question: does this issue belong in this repository or should we open another issue in the socket.io repo?).
Thanks a lot for your help!
Relevant versions in use
@nestjs/platform-socket.io@8.4.4
socket.io@4.4.1
@socket.io/redis-adapter@7.1.0
Reproduction code
/cc @fan711