According to socket.io examples:
To broadcast, simply add a
broadcast
flag toemit
andsend
method calls. Broad
You solution works if you are working in an evented environment where messages from the client trigger a response to all others, but not if you want the server to sent messages to all clients.
You can do this using io.sockets.emit
:
var io = require('socket.io').listen(80);
io.sockets.emit('message', { message: "Hello everyone!" });
However the documentation isn't clear for how to do this to a specific namespace. io.sockets
is just a shortcut to io.of('')
, as such you can broadcast to everyone on a namespace by calling io.of('/namespace').emit
:
var io = require('socket.io').listen(80);
io.of('/admins').emit('message', { message: "Hello admins!" });