Server-Sent Events vs Polling
Asked Answered
A

2

83

Is there a big difference (in terms of performance, browser implementation availability, server load etc) between HTML5 SSEs and straight up Ajax polling? From the server side, it seems like an EventSource is just hitting the specified page every ~3 seconds or so (though I understand the timing is flexible).

Granted, it's simpler to set up on the client side than setting up a timer and having it $.get every so often, but is there anything else? Does it send fewer headers, or do some other magic I'm missing?

Abdominal answered 22/2, 2012 at 15:18 Comment(0)
C
102

Ajax polling adds a lot of HTTP overhead since it is constantly establishing and tearing down HTTP connections. As HTML5 Rocks puts it "Server-Sent Events on the other hand, have been designed from the ground up to be efficient."

Server-sent events open a single long-lived HTTP connection. The server then unidirectionally sends data when it has it, there is no need for the client to request it or do anything but wait for messages.

One downside to Server-sent events is that since they create a persistent connection to the server you could potentially have many open connections to your server. Some servers handle massive numbers of concurrent connections better than others. That said, you would have similar problems with polling plus the overhead of constantly reestablishing those connections.

Server-sent events are quite well supported in most browsers, the notable exception of course being IE. But there are a couple of polyfills (and a jQuery plugin) that will fix that.

If you are doing something that only needs one-way communication, I would definitely go with Server-sent events. As you mentioned Server-sent events tend to be simpler and cleaner to implement on the client-side. You just need to set up listeners for messages and events and the browser takes care of low-level stuff like reconnecting if disconnected, etc. On the server-side it is also fairly easy to implement since it just uses simple text. If you send JSON encoded objects you can easily turn them into JavaScript objects on the client via JSON.parse().

If you are using PHP on the server you can use json_encode() to turn strings, numbers, arrays and objects into properly encoded JSON. Other back-end languages may also provide similar functions.

Commode answered 3/3, 2012 at 9:12 Comment(3)
But what about the resources on the server side? is not better 1 ajax request every 5 seconds than keeping a connection forever for every user?Turpitude
There are some caveats with using SSE, 2 of the more important are that they only accept GET requests and that it doesn't allow you to specify headers.And
One issue you will run into is max_children on Apache, see: "SSE in PHP - Apache hanging and not registering/serving any new requests" → https://mcmap.net/q/244266/-server-sent-events-sse-in-php-7-4-apache-hanging-and-not-registering-serving-any-new-requests/1066234Ideography
H
6

I would only add a higher perspective to what's been said, and that is that SSE is publish-subscribe model as opposed to constant polling in case of AJAX.

Generally, both ways (polling and publish-subscribe) are trying to solve the problem how to maintain an up-to-date state on the client.

1) Polling model

It is simple. The client (browser) first gets an initial state (page) and for it to update, it needs to periodically request the state (page or its part) and process the result into the current state (refresh whole page or render it inteligently into its part in case of AJAX).

Naturally, one drawback is that if nothing happens with the server state the resources (CPU, network, ...) are used unnecessarily. Another one is that even if the state changes the clients gets it only at the next poll period, not ASAP. One often needs to evaluate a good period time compromise between the two things.

Another example of polling is a spinwait in threading.

2) Publish-subscribe model

It works as follows:

  • (client first requests and shows some initial state)
  • client subscribes to the server (sends one request, possibly with some context like event source)
  • server marks the reference to the client to some its client reference repository
  • in case of an update of the state, server sends a notification to the client based on the reference to the client it holds; i.e. it is not a response to a request but a message initiated by the server
  • good clients unsubscribe when they are no more interested in the notifications

This is SSE, or within threading a waitable event, as another example. A natural drawback, as stated, is that the server must know about all its subscribed clients which, depending on an implementation, can be an issue.

Hengelo answered 15/1, 2017 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.