Socket.IO 1.4
Object.keys(io.sockets.sockets); gives you all the connected sockets.
Socket.IO 1.0
As of Socket.IO 1.0, the actual accepted answer isn't valid any more.
So I made a small function that I use as a temporary fix:
function findClientsSocket(roomId, namespace) { var res = [] // The default namespace is "/" , ns = io.of(namespace ||"/"); if (ns) { for (var id in ns.connected) { if(roomId) { var index = ns.connected[id].rooms.indexOf(roomId); if(index !== -1) { res.push(ns.connected[id]); } } else { res.push(ns.connected[id]); } } } return res; }
The API for no namespace becomes:
// var clients = io.sockets.clients(); // becomes: var clients = findClientsSocket(); // var clients = io.sockets.clients('room'); // all users from room `room` // becomes var clients = findClientsSocket('room');
The API for a namespace becomes:
// var clients = io.of('/chat').clients(); // becomes var clients = findClientsSocket(null, '/chat'); // var clients = io.of('/chat').clients('room'); // all users from room `room` // becomes var clients = findClientsSocket('room', '/chat');
Also see this related question, in which I give a function that returns the sockets for a given room.
function findClientsSocketByRoomId(roomId) { var res = [] , room = io.sockets.adapter.rooms[roomId]; if (room) { for (var id in room) { res.push(io.sockets.adapter.nsp.connected[id]); } } return res; }
Socket.IO 0.7
The 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: Since it seems the Socket.IO API is prone to breaking, and some solution rely on implementation details, it could be a matter of tracking the clients yourself:
var clients = []; io.sockets.on('connect', function(client) { clients.push(client); client.on('disconnect', function() { clients.splice(clients.indexOf(client), 1); }); });
socketis id,io.sockets.sockets[socket]is the socket.errand if you're using memory store it's alwaysnull..get('nickname', function(err, nickname) { ... })