简体   繁体   English

NodeJS Socket.io Server < - >服务器通信

[英]NodeJS Socket.io Server<-->Server communication

I'm trying to establish server to server communication in NodeJS (cluster architecture, separate VMs) with socket.io. 我正在尝试使用socket.io在NodeJS(集群架构,单独的VM)中建立服务器到服务器通信。 I try to use what is posted here http://socket.io/docs/using-multiple-nodes/ 我尝试使用这里发布的内容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.emit("message", "some example message")我可以用以下方式监听它:

 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: 当运行一个服务器(node_app)并在另一个服务器上发送事件时,我看到调试消息:

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.): 我想在每个node_app之间实现通信(用于缓存失效等):

在此输入图像描述

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. 对于那些寻求类似案例解决方案的人:我发现这篇文章http://code.tutsplus.com/tutorials/multi-instance-nodejs-app-in-paas-using-redis-pubsub--cms-22239这基本上回答了我的问题。 With Redis publish/subscribe mechanism I achieved what I wanted. 使用Redis发布/订阅机制,我实现了我想要的。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM