NodeJS Socket.io Server<-->Server communication

谁说我不能喝 提交于 2019-12-10 18:48:39

问题


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.):


回答1:


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. :)



来源:https://stackoverflow.com/questions/36874287/nodejs-socket-io-server-server-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!