I just started playing with socket.io and tried to make something super simple, like people connecting to a room.
I expected this to give me the number of customers in the room
io.sockets.on('connection', function (socket){
socket.on('create or join', function (channel) {
var numClients = io.sockets.clients(channel).length;
...
}
})
But it returns an error: Object #<Namespace> has no method 'clients'because something has changed in V.1. After some searches, I replaced the callback with another:
var clients = io.sockets.adapter.rooms[channel];
var numClients = 0;
if (typeof clients !== 'undefined'){
numClients = clients.length;
}
p(numClients);
p(clients);
socket.join(channel);
socket.emit('created', channel);
But no matter how many times I connect, I get 0 as the number of clients:
0
undefined
0
[ hSxLuOqrm5bX4UxmAAAA: true ]
0
[ hSxLuOqrm5bX4UxmAAAA: true, '2bjYS0lrUFySPM1OAAAB': true ]
What am I doing wrong?
PS here's how to get the number of customers in a room:
var numClients = (typeof clients !== 'undefined') ? Object.keys(clients).length : 0;
source
share