Take the 2-minute tour ×

I would like to broadcast a message to all open sockets as a result of a non-socket related event, e.g. as a result of a timeout. How do I do this? Should I keep an array of all open sockets and send them a message one by one? Is there a better way?

Note: The broadcasting example in socket.io guide sends a broadcast message in response to a socket connection, so it has a handle to a socket. Even then it says

Broadcasting means sending a message to everyone else except for the socket that starts it.

Edit To clarify my question - I want to "send a message" to all open sockets. This action is NOT triggered by any socket, so there is no "this socket". For example, some business logic on the server decides that an order is now executed. This information needs to be sent to all open sockets. What's the best way to do this? (This is not "broadcasting" as socket.io defines it.)

share|improve this question

2 Answers 2

up vote 1 down vote accepted

So basically you need to get all connected clients to your socket

var clients = io.sockets.clients(); // This returns an array with all connected clients

for ( i = 0; i < clients.length; i++ ) {
    clients[i].emit('event', { data: 'message' });

This will emit to all of your connected clients.

share|improve this answer
Thanks! That works beautifully. –  Naresh Jan 29 '13 at 2:43
This solution will only work with one application process. If you want to send a message to all of them it would require a lot more work. Checkout out bus.io on npm. You can build distributed apps over socket.io and redis. –  Nathan Romano Jul 2 at 17:30
socket.broadcast.emit('event name', { data : 'your data' });

It will broadcast to all open sockets, except this socket.

share|improve this answer
Hi Charles, this is not the answer I was looking for. Please see the clarification of my question. Thanks. –  Naresh Jan 27 '13 at 18:42

Your Answer


