Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  4] [ 3]  / answers: 1 / hits: 56088  / 8 Years ago, tue, november 1, 2016, 12:00:00

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?


More From » node.js

 Answers
42

There is no .join() method on the client side. Rooms are purely a server-side construct and the client knows nothing about them.



Your first block of code is the desired way to do things. You send the server a message of your design asking it to join the socket to a room and the .join() is executed on the server side.


[#60215] Saturday, October 29, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
denism

Total Points: 627
Total Questions: 96
Total Answers: 98

Location: Ivory Coast
Member since Sun, Mar 7, 2021
3 Years ago
;