Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket IO Rooms: Get list of clients in specific room

I'm trying to display a list of clients in a specific room. I just want to show their username, and not their socket id.

This where I'm at:

socket.set('nickname', "Earl");   socket.join('chatroom1'); console.log('User joined chat room 1);  var roster = io.sockets.clients('chatroom1'); for ( i in roster ) {    console.log('Username: ' + roster[i]);    } 

Haven't had any luck getting it to list Socket IDs or anything. Would like it to return the nicknames however.

like image 686
Taurian Avatar asked Aug 07 '13 02:08

Taurian


People also ask

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

the room name is dynamic, its come from client side. Edited to use the room variable. Updated: Now, you can get number of clients in each room by numClients[socket. room] .

How do I monitor Socket.IO traffic?

Use Monitor.io to observe connections and replay messages It shows a list of active Socket.io client connections for your application. You can use the monitoring interface to broadcast messages–either globally or to a specific client.

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.

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 .


2 Answers

In socket.IO 3.x

New to version 3.x is that connected is renamed to sockets and is now an ES6 Map on namespaces. On rooms sockets is an ES6 Set of client ids.

//this is an ES6 Set of all client ids in the room const clients = io.sockets.adapter.rooms.get('Room Name');  //to get the number of clients in this room const numClients = clients ? clients.size : 0;  //to just emit the same event to all members of a room io.to('Room Name').emit('new event', 'Updates');  for (const clientId of clients ) {       //this is the socket of each client in the room.      const clientSocket = io.sockets.sockets.get(clientId);       //you can do whatever you need with this      clientSocket.leave('Other Room')  } 

In socket.IO 1.x through 2.x

Please refer the following answer: Get list of all clients in specific room. Replicated below with some modifications:

const clients = io.sockets.adapter.rooms['Room Name'].sockets;     //to get the number of clients in this room const numClients = clients ? Object.keys(clients).length : 0;  //to just emit the same event to all members of a room io.to('Room Name').emit('new event', 'Updates');  for (const clientId in clients ) {       //this is the socket of each client in the room.      const clientSocket = io.sockets.connected[clientId];       //you can do whatever you need with this      clientSocket.leave('Other Room') } 
like image 179
Sony Mathew Avatar answered Sep 18 '22 21:09

Sony Mathew


Instead of going deep in socket/io object , You can use simple and standard way :

io.in(room_name).clients((err , clients) => {     // clients will be array of socket ids , currently available in given room }); 

For more detail DO READ

like image 44
Vivek Doshi Avatar answered Sep 20 '22 21:09

Vivek Doshi