You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When running on multiple nodes with socket.io-redis there is no way to emit a message to all sockets in a namespace except a specific socket if the socket you want to omit is not on that node.
Currently you can only do
socket.broadcast.to('room').emit('hi','all');
which won't work on multiple nodes because the socket is not available to all nodes.
Expected behaviour
It would be nice to have a way to broadcast to all sockets except a specific one a node that does not contain the socket we want to omit. I therefore propose to add a method .except(id) that can be used as
io.to('room').except('id').emit('hi','all');
Setup
OS: any
browser: any
socket.io version: any
Implementation
I would be happy to file a PR for this if this functionality would be accepted. The implementation could looke like this:
// In namespace.jsfunctionNamespace(server,name){this.name=name;this.server=server;this.sockets={};this.connected={};this.fns=[];this.ids=0;this.rooms=[];this.flags={};this.omit=[];this.initAdapter();}Namespace.prototype.except=function(id){this.omit.push(id);returnthis;};Namespace.prototype.emit=function(ev){// ...varrooms=this.rooms.slice(0);varflags=Object.assign({},this.flags);varexcept=this.omit.slice(0);// reset flagsthis.rooms=[];this.flags={};this.omit=[];this.adapter.broadcast(packet,{rooms: rooms,flags: flags,except: except});returnthis;};
This was implemented in 7de2e87 and included in socket.io@4.0.0.
Example:
io.except("room1").emit(/* ... */);// to all clients except the ones in "room1"io.to("room2").except("room3").emit(/* ... */);// to all clients in "room2" except the ones in "room3"socket.broadcast.except("room1").emit(/* ... */);// to all clients except the ones in "room1" and the sendersocket.except("room1").emit(/* ... */);// same as abovesocket.to("room4").except("room5").emit(/* ... */);// to all clients in "room4" except the ones in "room5" and the sender
You want to:
Current behaviour
When running on multiple nodes with socket.io-redis there is no way to emit a message to all sockets in a namespace except a specific socket if the socket you want to omit is not on that node.
Currently you can only do
which won't work on multiple nodes because the socket is not available to all nodes.
Expected behaviour
It would be nice to have a way to broadcast to all sockets except a specific one a node that does not contain the socket we want to omit. I therefore propose to add a method
.except(id)
that can be used asSetup
Implementation
I would be happy to file a PR for this if this functionality would be accepted. The implementation could looke like this:
As far as I know this works with socket.io-redis out of the box. See also socketio/socket.io-redis-emitter#87 for a similar feature for socket.io-emitter.
The text was updated successfully, but these errors were encountered: