简体   繁体   English

socketio与redis和haproxy进行服务器间通信

[英]socketio inter server communication with redis and haproxy

I'm working on a project which uses SocketIO and should be Horizontally scalable. 我正在使用SocketIO的项目中,并且应该可以水平扩展。 Im using 我正在使用

  • A Load Balancer using HAProxy 使用HAProxy的负载均衡器
  • Multiple Node Servers (2-4) 多节点服务器(2-4)
  • Database server(Redis and MongoDB) 数据库服务器(Redis和MongoDB)

I'm able to redirect my incoming Socket connections to Node servers using roundrobin method. 我可以使用roundrobin方法将传入的Socket连接重定向到Node服务器。 Socket connection is stable and if I use socket.emit() I'm receiving the data. 套接字连接是稳定的,如果我使用socket.emit(),我正在接收数据。 I'm also able to emit to the Other socket connection connected to the same Node server. 我还可以发出连接到同一节点服务器的其他套接字连接。

I'm facing issue in the following scenario: 我在以下情况下面临问题:

User A connected to Node server 1 and User B connected to Node Server 2 用户A连接到节点服务器1,用户B连接到节点服务器2

My intention is to store the Socket data in redis 我的意图是将套接字数据存储在Redis中

If User A wants to send some data to User B, how can I tell the Node server 2 to emit the data to User B from Node server 1 如果用户A要向用户B发送一些数据,如何告诉节点服务器2从节点服务器1向用户B发送数据

Please let me know how can I achieve this (with ref if possible). 请让我知道如何实现此目标(如果可能,请提供参考)。

Thanks in advance. 提前致谢。

This scenario is a match for the case Pub/Sub of Redis . 此方案与Redis Pub / Sub案例相匹配。

If you haven't already, you should try Pub/Sub . 如果还没有,请尝试Pub / Sub

Have a look at socket.io Redis adapter . 看一下socket.io Redis适配器 It should be exactly what you need. 它应该正是您所需要的。

clients() method in particular looks promising. 尤其是clients()方法看起来很有希望。 Keep in mind, that socket.io creates a unique room for each client. 请记住,socket.io为每个客户端创建一个唯一的空间。

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

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