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

Allow to emit message to several rooms at once #3313

Closed
1 task done
angel1st opened this issue Aug 21, 2018 · 4 comments
Closed
1 task done

Allow to emit message to several rooms at once #3313

angel1st opened this issue Aug 21, 2018 · 4 comments
Milestone

Comments

@angel1st
Copy link

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

For bug reports and feature requests for the Swift client, please open an issue there.

For bug reports and feature requests for the Java client, please open an issue there.

You want to:

  • request a feature

Current behaviour

Thanks to #2879 it is possible to join several rooms at once.

It would be nice to have the same for sending a message to multiple rooms.
Currently, we can achieve it with following code snippet:

['room1', 'room2', 'room3'].forEach(room => {
  // send message to all clients in the room, but the sender
  socket.broadcast.to(room).emit('message', 'nice game');
});

It would be much better if we can have it like that:

io.in(['room1', 'room2', 'room3']).emit('message', 'cool game');
socket.broadcast.to(['room1', 'room2', 'room3']).emit('message', 'nice game');
@vadibezdushnyi
Copy link

And I have a little addition.
io.to(socketId).ifNotIn('room').emit('message', 'cool game');

@Strate
Copy link

Strate commented Aug 22, 2018

At least we can handle multiple arguments. I use this kind of patched function:

io.to = (function(to) {
  return function patchedTo(...rooms) {
    return rooms.reduce(
      (thisArg, room) => to.call(thisArg, room),
      this
    )
  }
})(io.to);

@darrachequesne
Copy link
Member

Duplicate of #3048

@darrachequesne darrachequesne marked this as a duplicate of #3048 Feb 23, 2021
@darrachequesne
Copy link
Member

For future readers: this feature was implemented by 085d1de and included in [email protected].

Syntax:

io.to(["room1", "room2", "room3"]).emit(/* ... */);

socket.to(["room1", "room2", "room3"]).emit(/* ... */);

Documentation: https://socket.io/docs/v3/migrating-from-3-x-to-4-0/#Allow-to-pass-an-arrow-to-io-to

@darrachequesne darrachequesne added this to the 4.0.0 milestone Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants