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.
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] .
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.
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.
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 .
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') }
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
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