user3054852
user3054852

Reputation: 189

Socket.io room with most joined

Well, title says it all. How can I get the rooms with most joined sockets? A way to list them ordered by sockets would be neat. Also if there is a way, how efficient is it? And if it isn't, can one make an own efficient way?

Upvotes: 0

Views: 101

Answers (1)

robertklep
robertklep

Reputation: 203231

Not really tested, but I think this might work:

var roomNames = Object.keys(io.sockets.manager.rooms);
var sortedByNumberOfClients = roomNames.sort(function(room1, room2) {
  return io.sockets.manager.rooms[room2].length - io.sockets.manager.rooms[room1].length;
});
// sortedByNumberOfClients is an array of room names, sorted on
// number of clients in the room, largest room first.

I do believe that there's a default room with an empty string ('') as name that contains all clients, so you may want to skip that. Also, I think internally socket.io adds a leading / to each room name.

Upvotes: 1

Related Questions