Skip to content Skip to sidebar Skip to footer

Iterate Over Sockets In Socket.io V1? "...has No Method 'clients'"

Before I was able to write something like this: io.sockets.clients().forEach(function (socket) { socket.emit(signal,data); }); Now, I cannot and I get the error Object #

Solution 1:

If the info about all the connected sockets has to be send to a single socket then

for (var i in io.sockets.connected) {
        var s = io.sockets.connected[i];
        if (socket.id === s.id) {
           continue;
        }
        socket.emit('notify_user_state', s.notify_user_state_data)
    }

Solution 2:

I had got the same problem. Try using:

io.sockets.emit('newMessage', someCalculations());

Hope it helps

Solution 3:

This is my current solution:

var socketList = newArray();
io.on('connection',function(socket){
    socketList.push(socket);
});

setInterval(function(){
    socketList.forEach(function(){
        socket.emit('newMessage',someCalculations());
    });
},1000);

Solution 4:

If you want find out each connected clients. You can get them from engine.io

for (var sid in io.engine.clients) {
  io.to(sid).emit('your message', data);
}

io.engine.clients is a hash. The key is socket id and the value contain some useful info. e.g. request. If you use passport.socketio. You can get authed user from that place.

Post a Comment for "Iterate Over Sockets In Socket.io V1? "...has No Method 'clients'""