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

后端 未结 3 1343
忘掉有多难
忘掉有多难 2021-02-04 06:52

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 cas

3条回答
  •  孤独总比滥情好
    2021-02-04 07:16

    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.

提交回复
热议问题