ELB上的Socket io需要连接到应用程序服务器的所有实例

时间:2022-01-02 23:55:41

ELB上的Socket io需要连接到应用程序服务器的所有实例 I have a socket server application written on node js, hosted on Amazon EBS(Elastic bean stalk) which is managed by load balancer

我有一个在节点js上编写的套接字服务器应用程序,托管在Amazon EBS(Elastic bean stalk)上,由负载均衡器管理

Clients connects to the socket and pass specified id, say event_id my application creates room with name "event_event_id" and join the room. Different clients have different "event_id"

客户端连接到套接字并传递指定的id,比如event_id我的应用程序创建名为“event_event_id”的房间并加入房间。不同的客户有不同的“event_id”

A lambda function connects to the socket and emits an event named "event_push" to the application with necessary data such as event_id

lambda函数连接到套接字并向应用程序发出名为“event_push”的事件,并使用必要的数据(如event_id)

Whenever my socket application(in one of the instances in EBS) detects a push event with event_id, it broadcast the pushed data to all members of the room "event_event_id", thus all members gets notified of the event

每当我的套接字应用程序(在EBS中的一个实例中)检测到具有event_id的推送事件时,它将推送的数据广播到房间的所有成员“event_event_id”,因此所有成员都会收到有关该事件的通知

clients can connect any of the servers, decided by load balancer and sticky sessions can maintain the connection

客户端可以连接任何服务器,由负载均衡器和粘性会话决定可以保持连接

Now my problem: Is there a way to emit the "event_push" to only the server having a room named "event_event_id"

现在我的问题:有没有办法将“event_push”发送到只有名为“event_event_id”的房间的服务器

Qn: Is there a way to emit an event to all instances of the servers in EBS? OR Qn: Is there any alternate solutions?

问:有没有办法向EBS中的所有服务器实例发送事件?或Qn:还有其他解决方案吗?

1 个解决方案

#1


0  

What you can do is you can use Redis or mongoDb for saving some information about in your case event_event_id. You can save this information in data storage for example sample doc

你可以做的是你可以使用Redis或mongoDb来保存你的案例event_event_id中的一些信息。您可以将此信息保存在数据存储中,例如示例文档

{
   _id : ObjectId,
   userId : 23,
   event : event_event_id,
   server : 1
},
{
   _id : ObjectId,
   userId : 23,
   event : event_event_id,
   server : 2
}

Now what this will do is you will have the information that which event belongs to which server. Now if loadbalancer send request of server1 to server 2 and server 2 doesnt recognize that request you can create a communication channel between the servers(you can use socketio, microservice or anything) and redirect that request to server 1 so it can trigger that particular socket event.

现在,您将获得哪些事件属于哪个服务器的信息。现在,如果loadbalancer将server1的请求发送到服务器2并且服务器2不识别该请求,则可以在服务器之间创建通信通道(可以使用socketio,microservice或任何东西)并将该请求重定向到服务器1,以便它可以触发该特定套接字事件。

You have to delete the socket id from mongo or redis when disconnect event is triggered.

触发断开连接事件时,您必须从mongo或redis中删除套接字ID。

#1


0  

What you can do is you can use Redis or mongoDb for saving some information about in your case event_event_id. You can save this information in data storage for example sample doc

你可以做的是你可以使用Redis或mongoDb来保存你的案例event_event_id中的一些信息。您可以将此信息保存在数据存储中,例如示例文档

{
   _id : ObjectId,
   userId : 23,
   event : event_event_id,
   server : 1
},
{
   _id : ObjectId,
   userId : 23,
   event : event_event_id,
   server : 2
}

Now what this will do is you will have the information that which event belongs to which server. Now if loadbalancer send request of server1 to server 2 and server 2 doesnt recognize that request you can create a communication channel between the servers(you can use socketio, microservice or anything) and redirect that request to server 1 so it can trigger that particular socket event.

现在,您将获得哪些事件属于哪个服务器的信息。现在,如果loadbalancer将server1的请求发送到服务器2并且服务器2不识别该请求,则可以在服务器之间创建通信通道(可以使用socketio,microservice或任何东西)并将该请求重定向到服务器1,以便它可以触发该特定套接字事件。

You have to delete the socket id from mongo or redis when disconnect event is triggered.

触发断开连接事件时,您必须从mongo或redis中删除套接字ID。