I created a fairly trivial socket.io project to prove that something seems to be wrong with 'sockets.emit'.
I created a git here: https://github.com/dirkk0/minimalsocket
The lines in question are https://github.com/dirkk0/minimalsocket/blob/master/server.js#l17-21
// this should work but doesn't
io.sockets.emit(JSON.stringify(msg))
which doesn't work.
So as a workaroud I iterate through the clients to send a message, which works fine:
io.sockets.clients().forEach(function (socket) {
socket.send(JSON.stringify(msg));
});
I tested on Ubuntu 12.04 with node v0.10.24, npm 1.3.21, socket.io 0.9.16, express 3.4.7 and on MacOSX with node 0.10.12. Also Safari, Firefox, Chrome on MacOSX and Chrome on Windows7. The results are consistent.
Am I overlooking something obvious?
Thanks, Dirk
EDIT: yes, I overlooked that I had to give the channel an name (which for some reason isn't needed with .send), like in:
io.sockets.emit('message', JSON.stringify(msg))
socket.emit is not broken.