How do I get a list of connected sockets/clients with Socket.IO?
node.js, socket.io
Solution
In Socket.IO 0.7 you have a `clients` method on the namespaces. This returns an array of all connected sockets.
API for no namespace:
var clients = io.sockets.clients();
var clients = io.sockets.clients('room'); // all users from room `room`
For a namespace
var clients = io.of('/chat').clients();
var clients = io.of('/chat').clients('room'); // all users from room `room`
Note: This solution only works with a version prior to 1.0
From 1.x and above, please refer to getting how many people are in a chat room in socket.io.
Problem
I'm trying to get a list of all the sockets/clients that are currently connected. `io.sockets` does not return an array, unfortunately. I know I could keep my own list using an array, but I don't think this is an optimal solution for two reasons: Redundancy. Socket.IO already keeps a copy of this list. Socket.IO provides method to set arbitrary field values for clients (i.e: `socket.set('nickname', 'superman')`), so I'd need to keep up with these changes if I were to maintain my own list. What should I do?