Search code examples
javascriptnode.jswebsocketsocket.io

socket.io emit to everyone except for some certain clients


In my Node.js server, I have an array of client IDs, and I have a socket that I want to send to all connected clients except for the clients I have in that array, so I want to emit to all clients excluding some clients that i have defined by id, is that possible?


Solution

  • Answering my own question after some research.

    One thing you could do is to add the clients you want to exclude inside of a Scoket.io room and then emit to everyone except for that room, like shown below:

    io.except("room1").emit(/* ... */); // to all clients except the ones in "room1"
    

    Note that this feature was implemented in version [email protected]. here is documentation for it