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

后端 未结 3 1353
忘掉有多难
忘掉有多难 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 06:59

    From the perspective of a web developer, the difference between Websockets and a REST interface is semantics. REST uses a request/response model where every message from the server is the response to a message from the client. WebSockets, on the other hand, allow both the server and the client to push messages at any time without any relation to a previous request.

    Which technique to use depends on what makes more sense in the context of your application. Sure, you can use some tricks to simulate the behavior of one technology with the other, but it is usually preferably to use the one which fits your communication model better when used by-the-book.

    Server-sent events are a rather new technology which isn't yet supported by all major browsers, so it is not yet an option for a serious web application.

提交回复
热议问题