Reputation: 820
its a duplicate question, though i want to ask it. i want all the username & there socket.id who are currently connected to a room, how to get that,
i tried it with : var clients = io.sockets.clients('room1');
here is my code ref
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// store the username in the socket session for this client
socket.username = username;
// store the room name in the socket session for this client
// socket.myroom = 'room1';
// add the client's username to the global list
usernames[username] = username;
// send client to room 1
socket.join('room1');
socket.join('room2');
// echo to client they've connected
socket.emit('updatechat', 'room1', 'you have connected to room1');
socket.emit('updatechat', 'room2', 'you have connected to room2');
// echo to room 1 that a person has connected to their room
socket.broadcast.to('room1').emit('updatechat', 'room1', username + ' has connected to this room');
socket.broadcast.to('room2').emit('updatechat', 'room2', username + ' has connected to this room');
var clients = io.sockets.clients('room1');
console.log('socket.id :'+ socket.id);
console.log('projson :'+ clients);
console.log('projson :'+ clients.socket.id);
console.log('projson :'+ clients.socket);
});
here is the log
debug: set heartbeat interval for client lhF85vZH16MLuMxyuIFH
debug: client authorized for
debug: websocket writing 1::
debug: websocket writing 5:::{"name":"updatechat","args":["room1","you have connected to room1"]}
debug: websocket writing 5:::{"name":"updatechat","args":["room2","you have connected to room2"]}
debug: broadcasting packet
debug: websocket writing 5:::{"name":"updatechat","args":["room1","p3 has connected to this room"]}
debug: broadcasting packet
debug: websocket writing 5:::{"name":"updatechat","args":["room2","p3 has connected to this room"]}
socket.id :lhF85vZH16MLuMxyuIFH
**projson :[object Object],[object Object]**
projson :undefined
projson :undefined
how to get socketid & user name , any help will be appreciated , thank you
Upvotes: 0
Views: 3185
Reputation: 1409
To get the socket ids:
Object.keys(io.nsps['/'].adapter.rooms[roomName].sockets)
Upvotes: 0
Reputation: 2310
try
console.log(io.sockets.manager.rooms)
this will give you something like this:
{
"room1":[socketidOfUser1,socketidOfUser2],
"room2":[socketidOfUser3,socketidOfUser4],
}
see if this gives the information you need
Upvotes: 1