Update all clients using Socket.io? Update all clients using Socket.io? javascript javascript

Update all clients using Socket.io?


It's not actually sending an update to the other clients at all, instead it's just emitting to the client that just connected (which is why you see the update when you first load)

// socket is the *current* socket of the client that just connectedsocket.emit('users_count', clients); 

Instead, you want to emit to all sockets

io.sockets.emit('users_count', clients);

Alternatively, you can use the broadcast function, which sends a message to everyone except the socket that starts it:

socket.broadcast.emit('users_count', clients);


I found that using socket.broadcast.emit() will only broadcast to the current "connection", but io.sockets.emit will broadcast to all the clients. here the server is listening to "two connections", which are exactlly 2 socket namespaces

io.of('/namespace').on('connection', function(){    socket.broadcast.emit("hello");});io.of('/other namespace').on('connection',function(){/*...*/});

i have try to use io.sockets.emit() in one namespace but it was received by the client in the other namespace. however socket.broadcast.emit() will just broadcast the current socket namespace.


As of socket.io version 0.9, "emit" no longer worked for me, and I've been using "send"

Here's what I'm doing:

Server Side:

var num_of_clients = io.sockets.clients().length;io.sockets.send(num_of_clients);

Client Side:

ws = io.connect...ws.on('message', function(data){var sampleAttributes = fullData.split(',');if (sampleAttributes[0]=="NumberOfClients")        {            console.log("number of connected clients = "+sampleAttributes[1]);        }});