2

How can you retrieve all the connected clients in a room using socket.io 2.0


I already tried the solution in a similar question: https://stackoverflow.com/a/45160538/2189845

var sockets = io.in("room_name")
 Object.keys(sockets.sockets).forEach((item) => {
 console.log("TODO: Item:", sockets.sockets[item].id)            
})

But this loops over all socket connections, regardless of the room. So the code above gives the same result as:

Object.keys(io.sockets.sockets).forEach((item) => {
    console.log("general socket: Item:", item);
});
Dries Cleymans
  • 770
  • 8
  • 20

3 Answers3

2

Unfortunately, this answer doesn't completely satisfy OPs question. If you are using socket.io-redis to manage socket connections you can get connected clients like

io.of('/').adapter.clients((err, clients) => {
  console.log(clients); // an array containing all connected socket ids
});

io.of('/').adapter.clients(['room1', 'room2'], (err, clients) => {
  console.log(clients); // an array containing socket ids in 'room1' and/or 'room2'
});

// you can also use

io.in('room3').clients((err, clients) => {
  console.log(clients); // an array containing socket ids in 'room3'
});

this is from : https://github.com/socketio/socket.io-redis#redisadapterclientsroomsarray-fnfunction

kevzettler
  • 4,783
  • 15
  • 58
  • 103
  • 1
    This works so I accepted this as the correct answer. When you only run 1 node server it feels like overkill.. You need to run an extra redis server, only for having a list of connected clients. Wish there was an easier solution but i'll go for this one. It makes us ready to upscale to multiple node servers i guess. – Dries Cleymans Aug 03 '17 at 06:40
  • How do you keep track of user information with socket id ? A redis key value pair can be used but If the server restarts and a client reconnects, a new socket id is created and the old one remains in redis, causing a memory leak. – Koder Nov 15 '17 at 06:35
0

Try:

io.sockets.clients('room_name').forEach(function(item) {
    console.log(item.id);//any property of socket you want to print
});

Or you could try:

var clients = io.sockets.adapter.rooms['room_name'].sockets;
for (var clientId in clients ) {

     //this is the socket of each client in the room.
     var SocketofClient = io.sockets.connected[clientId];
     console.log(SocketofClient.id);
}
wrangler
  • 3,454
  • 1
  • 19
  • 28
  • 7
    Those are not valid any longer in 2.0. I receive 'TypeError: fn.bind is not a function' for the first one and 'TypeError: Cannot read property 'sockets' of undefined' for the second solution. – Dries Cleymans Aug 02 '17 at 16:16
0

Check this code which is run in socket.io 2.0

  io.sockets.in(roomID).clients(function(err, clients) {
                            clients.forEach(client => {
                                let user = io.sockets.connected[client];
                                console.log("Connected Client ", user.displayName);
                            });
                        });
Rishabh Rawat
  • 1,083
  • 9
  • 15