Trigger a nodeJS socket.io server to broadcast via port 80
Question
Realtime Update Mechanism : user writes -> php save it in mysql db -> php send info to nodeJS -> nodeJS send the change all subscribers -> others can notice it in realtime.
The Socket.io server works well and runs on port 8080. I have node http server running on port 80. How can I get a trigger message on the http server and send the message to all socket.io clients?
var io = require('socket.io').listen(8080);
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Message Broadcast\n');
console.log("Message Sent");
//DOES NOT SEEM TO WORK, error
io.broadcast("Messages");
//No error but no messages
io.emit("Message");
}).listen(80, "0.0.0.0");
/**
Other socket.io code which works well..
....
Solution
In Socket.IO 0.7 you should use the following instead to send to all connected sockets without a specific socket reference:
io.sockets.send("Messages")
Note that this is different to broadcast
since that would send to all sockets except the socket that initiated the connection - of course you require a sending socket reference which is why your current code wouldn't suffice.
Quote from the wiki page: Socket.IO/Wiki
If you want to send a message to everyone you can reference
io.sockets
:
io.sockets.send('message');
io.sockets.emit('event');