7
votes

I am working on a node.js based Web application that needs to be able to push data down to the browser. The Web app will be sitting behind a load balancer.

Assuming the Web app has a POST REST API as:

/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.

Now assuming I have two servers behind the load balancer, with the Web app running. A browser client connects to server 1 to listen on the event. 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?

And what if I am using auto scaling, with dynamic number of servers behind the load balancer?

1
How are you sending data back to your browser from your Node.js based app ? WebSockets ? I think you need to maintain some sort of connection before sending the data..T8y
I started with Websocket, but it has issues with my load balancer. So I would like to try Server Sent Event. It is basically that we keep the req object forever, so we can write back to client later. Either Websocket or server sent events, I think it is a direct connection to one of the servers behind the load balancer, and it needs to be notified if an event happens on another server.user1670498
Are you still having issues?baynezy

1 Answers

4
votes

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.

Server Sent Events with Node JS