Should we prefer SSE + REST over websocket when using HTTP/2?

Guillaume D. picture Guillaume D. · Sep 7, 2015 · Viewed 7.8k times · Source

When using websocket, we need a dedicated connection for bidirectionnel communication. If we use http/2 we have a second connection maintained by the server.

In that case, using websocket seems to introduce an unecessary overhead because with SSE and regular http request we can have the advantage of bidirectionnal communication over a single HTTP/2 connection.

What do you think?

Answer

sbordet picture sbordet · Sep 7, 2015

Using 2 streams in one multiplexed HTTP/2 TCP connection (one stream for server-to-client communication - Server Sent Events (SSE), and one stream for client-to-server communication and normal HTTP communication) versus using 2 TCP connections (one for normal HTTP communication and one for WebSocket) is not easy to compare.

Probably the mileage will vary depending on applications.

Overhead ? Well, certainly the number of connections doubles up. However, WebSocket can compress messages, while SSE cannot.

Flexibility ? If the connections are separated, they can use different encryptions. HTTP/2 typically requires very strong encryption, which may limit performance. On the other hand, WebSocket does not require TLS.

Does clear-text WebSocket work in mobile networks ? In the experience I have, it depends. Antiviruses, application firewalls, mobile operators may limit WebSocket traffic, or make it less reliable, depending on the country you operate.

API availability ? WebSocket is a wider deployed and recognized standard; for example in Java there is an official API (javax.websocket) and another is coming up (java.net.websocket).

I think SSE is a technically inferior solution for bidirectional web communication and as a technology it did not become very popular (no standard APIs, no books, etc - in comparison with WebSocket). I would not be surprised if it gets dropped from HTML5, and I would not miss it, despite being one of the first to implement it in Jetty.

Depending on what you are interested in, you have to do your benchmarks or evaluate the technology for your particular case.