I have an application where a user can get a list of chat rooms they are in, they can then click into any specific chat room they want. Currently the socket joins the room when the user clicks into a specific room and the socket leaves the room when the user goes back to the main list of chat rooms. This means that when the user is on the chat list page their socket is not in any rooms (bar the default room that the connection event makes) and thus new messages are not pushed to them in real time when on this page.
I plan that when the user loads the chat list page, they join all rooms at that point.
I can see that you can emit to multiple rooms like this:
io.to('room1').to('room2').to('room3').emit('some event');
Is there a way to join multiple rooms at the same time in socket?
socket.join('room1').join('room2').join('room3')
?
or
socket.join('room1', 'room2', 'room3')
Or am I best off doing something like:
rooms = ['room1', 'room2', 'room3'];
rooms.forEach(room => {
socket.join(room)
});
Joining and leaving In that case, a union is performed: every socket that is at least in one of the rooms will get the event once (even if the socket is in two or more rooms). In that case, every socket in the room excluding the sender will get the event. To leave a channel you call leave in the same fashion as join .
socket.io rooms are a lightweight data structure. They are simply an array of connections that are associated with that room. You can have as many as you want (within normal memory usage limits). There is no heavyweight thing that makes a room expensive in terms of resources.
Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).
Updated: Now, you can get number of clients in each room by numClients[socket. room] .
Socket.IO - Rooms. Within each namespace, you can also define arbitrary channels that sockets can join and leave. These channels are called rooms. Rooms are used to further-separate concerns. Rooms also share the same socket connection like namespaces.
One thing to keep in mind while using rooms is that they can only be joined on the server side. You can call the join method on the socket to subscribe the socket to a given channel/room. For example, let us create rooms called 'room-<room-number>' and join some clients.
Within each namespace, you can also define arbitrary channels that sockets can join and leave. These channels are called rooms. Rooms are used to further-separate concerns. Rooms also share the same socket connection like namespaces. One thing to keep in mind while using rooms is that they can only be joined on the server side.
Currently the socket joins the room when the user clicks into a specific room and the socket leaves the room when the user goes back to the main list of chat rooms.
Yes, just put the array into the join function:
rooms = ['room1', 'room2', 'room3'];
socket.join(rooms);
Docs: https://socket.io/docs/server-api/#socket-join-rooms-callback
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With