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