-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can emit in a room exclude particular socket id ? not sender. #3657
Comments
Migrated from the Engine.IO repository. No, it's not currently possible, but it shouldn't be that hard to implement. Could you please explain your use case? EDIT: it's now available, see below |
This was implemented in 7de2e87 and included in 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 sender
socket.except("room1").emit(/* ... */); // same as above
socket.to("room4").except("room5").emit(/* ... */); // to all clients in "room4" except the ones in "room5" and the sender Documentation: https://socket.io/docs/v3/migrating-from-3-x-to-4-0/#Allow-excluding-specific-rooms-when-broadcasting |
New syntax: ``` io.except("room1").emit(...); io.to("room1").except("room2").emit(...); socket.broadcast.except("room1").emit(...); socket.to("room1").except("room2").emit(...); ``` Related: - socketio#3629 - socketio#3657
Hey, do you have any ideas on how to implement this? I have a use case for this, and would like to hear your thoughts. |
@pluiz-briteris this has been implemented in version 4.0.0, please see my answer above. |
I may be mistaken, but isn't that for excluding a room? The original question says excluding a particular socket by id. E.g. I use the underlying server to emit but exclude a given socket of a given room, by this socket id. Or is this happening somehow in one of the examples you shared? |
Sorry, I wasn't clear enough! The socket id can be used as a room: io.except(socketId).emit("some event"); Which is equivalent to: socket.broadcast.emit("some event"); // if you have access to the socket object Similarly, in a particular room: io.to("some room").except(socketId).emit("some event"); |
Thank you for that! It probably cover my use case. Do we have an example like this one in the docs? If not, it would be nice, happy to contribute if needed. |
@pluiz-briteris actually, we try to discourage users to use the socket ID in their application, since it's an ephemeral identifier which is lost upon reconnection. Instead you should rather use a session ID or a user ID for this use case. |
In my use case specifically, this won't be an issue, due to that I think it's worth having this addressed in documentation/js docs (with the reconnection point), because other than this issue, I would have never figured out that this is possible and this helps me a lot with what I need to do... |
Note: for support questions, please use one of these channels: stackoverflow or slack
You want to:
Current behaviour
Steps to reproduce (if the current behaviour is a bug)
Expected behaviour
Setup
Other information (e.g. stacktraces, related issues, suggestions how to fix)
The text was updated successfully, but these errors were encountered: