Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get socket.io number of clients in room?

my socket.io version 1.3.5

I want to get number of clients in particular room.

This is my code.

 socket.on('create or join', function (numClients, room) {
            socket.join(room);
    });

I use this code for get clients in room :

console.log('Number of clients',io.sockets.clients(room));
like image 707
yathavan Avatar asked Jul 17 '15 04:07

yathavan


People also ask

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 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).

How many messages per second can Socket.IO handle?

Load benchmarks Here we can see that the HTTP benchmark peaks at about~950 requests per second while Socket.io serves about ~3900 requests per second.

How do I find my client ID socket?

socket.id can be obtained after 'connect' event. Note, the socket.id on the server is not made available to the client, so if you want the id from the server, then you can send it to the client yourself in a message.


2 Answers

This works for version 3

io.sockets.adapter.rooms.get(roomName).size
like image 164
the artist Avatar answered Nov 15 '22 07:11

the artist


To get the number of clients in a room you can do the following:

    function NumClientsInRoom(namespace, room) {
      var clients = io.nsps[namespace].adapter.rooms[room];
      return Object.keys(clients).length;
    }

This variable clients will hold a object where each client is a key. Then you just get the number of clients (keys) in that object.

If you haven't defined a namespace the default one is "/".

like image 25
SejH Avatar answered Nov 15 '22 07:11

SejH