Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting how many people are in a chat room in socket.io [duplicate]

I have this code right now that sets the nick and room:

io.sockets.on('connection', function(client){     var Room = "";     client.on("setNickAndRoom", function(nick, fn){         client.join(nick.room);         Room = nick.room;         client.broadcast.to(Room).emit('count', "Connected:" + " " + count);         fn({msg :"Connected:" + " " + count});     }); 

I wanted to know how I could get how many people are connected to a specific chatroom...like Room.length

client side :

function Chat(){     this.socket = null;     this.Nickname = "";     this.Room = "";     var synched = $('#syncUp');     this.Connect = function(nick, room){          socket =  io.connect('http://vybeing.com:8080');             Nickname = nick;         Room = room;         //conectarse         socket.on('connect',function (data) {             socket.emit('setNickAndRoom', {nick: nick, room: room}, function(response){                 $("#connection").html("<p>" + response.msg + "</p>");             });         }); } 

I found this, but it gives undefined:

count = io.rooms[Room].length; 
like image 464
re1man Avatar asked Feb 19 '12 19:02

re1man


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. Show activity on this post.

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?

Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok. I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.

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.


2 Answers

For socket.io versions >= 1.0:

Note that rooms became actual types with a .length property in 1.4, so the 1.4.x method should be stable from now on. Barring breaking changes to that type's API, of course.

To count all clients connected to 'my_room':

1.4+:

var room = io.sockets.adapter.rooms['my_room']; room.length; 

1.3.x:

var room = io.sockets.adapter.rooms['my_room']; Object.keys(room).length; 

1.0.x to 1.2.x:

var room = io.adapter.rooms['my_room']; Object.keys(room).length; 

This is assuming you're running with the default room adapter on a single node (as opposed to a cluster). Things are more complicated if you're in a cluster.


Other related examples:

  • Count all clients connected to server:

    var srvSockets = io.sockets.sockets; Object.keys(srvSockets).length; 
  • Count all clients connected to namespace '/chat':

    var nspSockets = io.of('/chat').sockets; Object.keys(nspSockets).length 
like image 72
kdbanman Avatar answered Nov 07 '22 01:11

kdbanman


If you're using version < 1,

var clients = io.sockets.clients(nick.room); // all users from room

like image 33
Sơn Trần-Nguyễn Avatar answered Nov 07 '22 01:11

Sơn Trần-Nguyễn