NodeJS Socket.io Server & # 8596; Server connection
I am trying to establish server-to-server communication in NodeJS (cluster architecture, separate virtual machines) using socket.io. I am trying to use what is posted here http://socket.io/docs/using-multiple-nodes/
var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
So my guess is (possibly wrong) that when executed, io.emit("message", "some example message")
I can listen to it with:
io.on('connection', function(socket){
console.log("io.on");
socket.on('message', function(msg){
console.log('message: ' + msg);
});
});
when starting one server (node_app) and sending an event on the other, I see debug messages:
socket.io-parser encoding packet {"type":2,"data":["message","some example message"],"nsp":"/"} +6s
socket.io-parser encoded {"type":2,"data":["message","some example message"],"nsp":"/"} as 2["message","some example message"] +0ms
I want to achieve communication between each node_app
(for invalidating the cache, etc.):
source to share
For anyone looking for a solution for a similar case: I found this article http://code.tutsplus.com/tutorials/multi-instance-nodejs-app-in-paas-using-redis-pubsub--cms-22239 . which essentially answered my question. Thanks to the Redis publish / subscribe mechanism, I achieved what I wanted. :)
source to share