Is it possible to send messages to multiple rooms using socket.io?
Sending to 1 room:
io.sockets.in(room).emit("id", {})
Sending to N rooms:
io.sockets.in(room1, room2, roomN).emit("id", {})
Is it possible to send messages to multiple rooms using socket.io?
Sending to 1 room:
io.sockets.in(room).emit("id", {})
Sending to N rooms:
io.sockets.in(room1, room2, roomN).emit("id", {})
Updated as of Socket.IO v2.0.3
https://socket.io/docs/emit-cheatsheet/
The sockets.in method only accepts one room as an argument, so to broadcast to multiple rooms you would have to reset the room, in between emissions. Something like this should work:
Yes, it's possible to emit to multiple rooms altogether. From the tests:
That's because when you use
to
orin
you're appending the room to the list of rooms to be targeted. From the source code (lib/socket.js):