简体   繁体   English

Node.js 服务器使用负载均衡器发送事件

[英]Node.js Server Sent Events with Load Balancer

I am working on a node.js based Web application that needs to be able to push data down to the browser.我正在开发一个基于 node.js 的 Web 应用程序,它需要能够将数据推送到浏览器。 The Web app will be sitting behind a load balancer. Web 应用程序将位于负载平衡器后面。

Assuming the Web app has a POST REST API as:假设 Web 应用程序有一个 POST REST API,如下所示:

/update_client /更新客户端

and assuming there is a third-party application calls this API to push some data to the Web app, and then the Web app pushes the data down to the browser.并且假设有第三方应用调用这个API向Web应用推送一些数据,然后Web应用将数据向下推送到浏览器。

Now assuming I have two servers behind the load balancer, with the Web app running.现在假设我在负载平衡器后面有两台服务器,Web 应用程序正在运行。 A browser client connects to server 1 to listen on the event.浏览器客户端连接到服务器 1 以侦听事件。 Then the other applications hits the /update_client API at the server 2. Now since the two activities happen on two different servers, how can server 2 notify server 1 to send the data to the connected clients?然后其他应用程序在服务器 2 上访问 /update_client API。既然这两个活动发生在两个不同的服务器上,服务器 2 如何通知服务器 1 将数据发送到连接的客户端?

And what if I am using auto scaling, with dynamic number of servers behind the load balancer?如果我使用自动缩放,负载平衡器后面有动态数量的服务器怎么办?

You need to have some kind of shared resource behind the servers so they all know about updates.您需要在服务器后面拥有某种共享资源,以便他们都知道更新。 I show how to use Redis Pub / Sub for this in a blog post I wrote recently.我在我最近写的一篇博客文章中展示了如何使用 Redis Pub/Sub 来实现这一点。

Server Sent Events with Node JS 使用 Node JS 的服务器发送事件

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

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