Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all the clients connected to a room in Socket.io version > 1

After the io.sockets.clients() method has been depreciated from the later versions of Socket.io, and after my research couldn't find any documentation on the socket.io offical web.

Morever, it gives the type error for clients() method as below:

TypeError: undefined is not a function

Has anyone figured out how to list all the connected clients in a room with the later versions of Socket.io?

like image 524
Zander17 Avatar asked Jun 11 '14 00:06

Zander17


People also ask

How do I find out how many users are in a Socket.IO room?

You use get(roomId) to get the value of that roomId in a Map and you use . size to the number of elements in a set unlike . length for number of elements in an array.

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.

How do I check Socket.IO version?

You can check the socket.io version in node_modules->socket.io->package. json and there will be "version": "your version of socket.io".

How many players can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).


1 Answers

To get socket IDs of the clients connected to a room use this code:

var namespace = '/';
var roomName = 'my_room_name';
for (var socketId in io.nsps[namespace].adapter.rooms[roomName]) {
    console.log(socketId);
}

Edit:

To get socket by socket ID you may try this:

var socket = io.sockets.connected[socketId];
like image 197
Oleg Avatar answered Oct 13 '22 21:10

Oleg