NodeJS Socket.io Server<-->服务器通信 [英] NodeJS Socket.io Server<-->Server communication

查看:62
本文介绍了NodeJS Socket.io Server<-->服务器通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 socket.io 在 NodeJS(集群架构,单独的虚拟机)中建立服务器到服务器的通信.我尝试使用这里发布的内容

解决方案

对于那些正在寻找类似案例解决方案的人:我发现这篇文章 http://code.tutsplus.com/tutorials/multi-instance-nodejs-app-in-paas-using-redis-pubsub--cms-22239 基本上回答了我的问题.使用 Redis 发布/订阅机制,我实现了我想要的.:)

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

解决方案

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

这篇关于NodeJS Socket.io Server<-->服务器通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆