node.jsredissocket.ioscalabilitysocket.io-redis

NodeJS Socket.io Server<-->Server communication


I'm trying to establish server to server communication in NodeJS (cluster architecture, separate VMs) with socket.io. I try 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 I assume (probably wrong) that when doing io.emit("message", "some example message") I can listen for it with:

 io.on('connection', function(socket){
  console.log("io.on");
  socket.on('message', function(msg){
    console.log('message: ' + msg);
  });
});

when run one server (node_app) and send event on the another one 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 cache invalidation etc.):

enter image description here


Solution

  • For those who will be looking for solution to 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. With Redis publish/subscribe mechanism I achieved what I wanted. :)