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 connected
socket.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);

Leave a Comment