Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server-Sent Events costs at server side

Tags:

If I understand the Server-Sent Events principle correctly, each time a client registers to an EventSource, it actually opens a new HTTP connection to the resource managing the event. Contrary to other HTTP requests, the connection stays alive so the server process/thread dedicated to this client keeps running until the client disconnects.

What if we have 1000 clients connected to an application using SSE? Would we have 1000 processes/threads (doing the same thing) running concurrently just to handle the SSEs? I guess I'm wrong but if I'm not, is it really more efficient than the usual AJAX polling method where at least the server doesn't need to run that many processes/threads concurrently?

like image 670
ouno Avatar asked Jan 08 '13 22:01

ouno


People also ask

What is a server side event?

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.

How does server side 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.

Should I use server-sent events?

Server-sent events is the perfect solution for real-time applications. it is lightweight, fast, easy to implement and it provides low latency. All source code is available on Github Repository.

How are server-sent events implemented?

A connection over SSE typically begins with client-initiated communication between client and server. The client creates a new JavaScript EventSource object, passing the URL of an endpoint to the server over a regular HTTP request. The client expects a response with a stream of event messages over time.


2 Answers

Yes, each client keeps the connection open as long as it can. With 1000 concurrent users you'd have 1000 TCP/IP connections open.

However, whether each connection uses a thread depends on the server.

Apache typically keeps a thread running for each connection, so it's pretty expensive. With Apache it's best to disable KeepAlive and use polling.

OTOH with event-based servers like node.js you can have just one process that manages all connections, so cost of each connection is much lower and you should be able to keep thousands of connections open easily.

The cool thing about SSE is that you can use it to do polling as well. It has retry: directive that specifies how long client should wait before reconnecting (polling) again. Just send that and close the connection when you want polling.

like image 119
Kornel Avatar answered Oct 18 '22 04:10

Kornel


It depends on the threading model of the server. Apache defaults to one thread (or process) per connection so, even if the threads aren't doing very much (as is expected with an SSE connections), they sit there using up resources.

Servers like Nginx have a slightly different model, each thread handles multiple requests asynchronously. So stuff like SSE and WebSockets is far more efficient.

Apache can be made to perform more like Nginx and similar servers.

like image 25
robertc Avatar answered Oct 18 '22 03:10

robertc