Skip to content
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

Closed
2 tasks done
redchallengers opened this issue Mar 15, 2019 · 9 comments
Closed
2 tasks done

Can emit in a room exclude particular socket id ? not sender. #3657

redchallengers opened this issue Mar 15, 2019 · 9 comments
Labels
enhancement New feature or request
Milestone

Comments

@redchallengers
Copy link

Note: for support questions, please use one of these channels: stackoverflow or slack

You want to:

  • report a bug
  • request a feature

Current behaviour

Steps to reproduce (if the current behaviour is a bug)

Expected behaviour

Setup

  • OS:
  • browser:
  • engine.io version:

Other information (e.g. stacktraces, related issues, suggestions how to fix)

@redchallengers redchallengers changed the title can send in room exclude perticular socket id? Can Emit in a room exclude particular socket id ? not sender. Mar 15, 2019
@redchallengers redchallengers changed the title Can Emit in a room exclude particular socket id ? not sender. Can emit in a room exclude particular socket id ? not sender. Mar 15, 2019
@darrachequesne darrachequesne transferred this issue from socketio/engine.io Oct 5, 2020
@darrachequesne darrachequesne added the enhancement New feature or request label Oct 5, 2020
@darrachequesne
Copy link
Member

darrachequesne commented Oct 5, 2020

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

darrachequesne pushed a commit that referenced this issue Mar 1, 2021
New syntax:

```
io.except("room1").emit(...);
io.to("room1").except("room2").emit(...);

socket.broadcast.except("room1").emit(...);
socket.to("room1").except("room2").emit(...);
```

Related:

- #3629
- #3657
@darrachequesne
Copy link
Member

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 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

@darrachequesne darrachequesne added this to the 4.0.0 milestone Mar 11, 2021
dzad pushed a commit to dzad/socket.io that referenced this issue May 29, 2023
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
@plu-briteris-rl
Copy link

plu-briteris-rl commented Sep 12, 2023

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?

@darrachequesne

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.

darrachequesne added a commit to socketio/socket.io-website that referenced this issue Sep 13, 2023
@darrachequesne
Copy link
Member

@pluiz-briteris this has been implemented in version 4.0.0, please see my answer above.

@plu-briteris-rl
Copy link

plu-briteris-rl commented Sep 13, 2023

@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?

@darrachequesne
Copy link
Member

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");

@plu-briteris-rl
Copy link

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.

@darrachequesne
Copy link
Member

@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.

@plu-briteris-rl
Copy link

plu-briteris-rl commented Sep 14, 2023

@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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants