Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Join multiple rooms at once Socket.io

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)
});
like image 500
Conor Avatar asked Jul 08 '20 12:07

Conor


People also ask

Can socket join multiple rooms?

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 .

How many rooms can Socket.IO handle?

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.

How many concurrent connections can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

How do you get Socket.IO number of clients in a room?

Updated: Now, you can get number of clients in each room by numClients[socket. room] .

What are rooms in socket Io?

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.

How do I join a room in socket?

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.

What is the difference between sockets and 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.

How do sockets work in a chat room?

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.


1 Answers

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

like image 188
Jelle Avatar answered Oct 19 '22 05:10

Jelle