Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how the server send message SSE worked in multiple server instance environments

I have a question on how to make SSE worked in multiple server environments.

In UI, there are two steps:

1. source = new EventSource('http://localhost:3000/stream');
   source.addEventListener('open', function(e) {
      $("#state").text("Connected")
    }, false);
  1. user in UI can post to api to update data

  2. after user post to api, server is sending event to UI to udpate UI

In one server environement, this worked perfect fine, no problem at all.

But in multi server instance environments, this won't be working. For example, I have two server instance, and UI subscribed to server 1, then server 1 is remembering the connection, but data update is from server 2, when data is changed, there is no connection for SSE in server 2. Then in this senario, how can server 2 send SSE to UI?

In order to make SSE working in multiple server environments, do we need to adopt any saving solution to save the connection information so that any server instance can send SSE accurately to UI?

Let me clarify this more: yes, both service 1 and service 2 are behind load balancer, they do not have to have same URL. UI is pure frontend end application, can even be mobile app. So, if UI is sending a eventSource request to LB of server1, then only this instance can use this connection to send event back to UI, right? But if we have multiple instance of server 1, that means any server 1 instance other than current one can NOT send event back to UI. I believe this is the limitation of SSE unless the connection can be shared among all the instances. But how.

Thanks

like image 811
user3006967 Avatar asked Apr 22 '17 00:04

user3006967


People also ask

How does SSE application work?

SSE is designed to use the JavaScript EventSource API in order to subscribe to a stream of data in any popular browser. Through this interface a client requests a particular URL in order to receive an event stream. SSE is commonly used to send message updates or continuous data streams to a browser client.

How does server-sent events work?

So what are Server-Sent Events? A client subscribes to a “stream” from a server and the server will send messages (“event-stream”) to the client until the server or the client closes the stream. It is up to the server to decide when and what to send the client, for instance, as soon as data changes.

What's the full form of SSE write how an SSE based application works?

Server-Sent Events (SSE) is a server push technology enabling a client to receive automatic updates from a server via an HTTP connection, and describes how servers can initiate data transmission towards clients once an initial client connection has been established.

What is SSE (Server-Sent Events)?

Server-Sent Events (SSE) allows the client to receive automatic updates from a server via HTTP connection. The client initiates the SSE connection and the server uses the event source protocol to send updates to the client. The client will receive updates from the server, but it can’t send any data to the server after the initial handshake.

How does SSE work on a server?

For the SSE to work, the server needs to tell the client that the response’s content-type is text/eventstream. Next, the server receives a regular HTTP request, and leaves the HTTP connection open until no more events are left or until the timeout occurs.

What is Server-Sent Events (SSE) for ESP8266?

We also have a similar Server-Sent Events guide for the ESP8266. Server-Sent Events (SSE) allows the client to receive automatic updates from a server via HTTP connection. The client initiates the SSE connection and the server uses the event source protocol to send updates to the client.

How does SSE handle push notifications?

To meet this goal, SSE introduces two components: a new EventSource interface in the browser, which allows the client to receive push notifications from the server as DOM events, and the “event stream” data format, which is used to deliver the individual updates.


1 Answers

If you have two servers, with different URLs, make one SSE connection (from each client) to each server.

Be aware of CORS restrictions, i.e. the same origin policy. (It works identically to xhr2 CORS, so fairly easy to google; my book also covers it in detail, chapter 9.)

If you have two servers behind a load balancer, which is presenting a single URL to the clients, then you just have to make sure the load balancer is configured correctly. I.e. to always pass through that socket to the correct server. If a back-end server dies, and needs replacing, the load balancer should close the SSE socket; the client will then auto-reconnect, and get a new back-end server.

The multiple servers behind a load balancer, should either be having their own data push socket connections to a master data source, or should all be polling the master data source.

like image 191
Darren Cook Avatar answered Jan 02 '23 22:01

Darren Cook