Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining socket io room on connect

I am trying to make a logged in user to join a certain socket.io room on connect. According to any examples I found on the net I seem to have emit some action from client to be able to join some room. Something like:

socket.on('connect', function() {    socket.emit('join', 'room1'); }); 

Server.js:

io.sockets.on('connection', function(socket) {     socket.on('join', function(room) {         socket.join(room);     }); }); 

And according to most tutorials it should work.

What I am thinkin to do is to:

socket.on('connect', function() {    socket.join('room1'); }); 

But It does not seem to work as whatever msg I emit from server are not caught on client. Any idea, what am I doing wrong? Is it possible in general?

like image 292
ArkadyB Avatar asked Nov 01 '16 20:11

ArkadyB


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


1 Answers

This should be what you need. Feel free to pass in whatever room name you want through the client. Only the server can handle assigning a socket to a room.

Server:

io.sockets.on('connection', function(socket) {         socket.on('join', function(room) {         socket.join(room);     }); }); 

Client:

socket.emit('join', roomNum); 
like image 132
Bk Razor Avatar answered Sep 22 '22 12:09

Bk Razor