HTTP/2 or Websockets for low latency client to server messages

前端 未结 3 1306
臣服心动
臣服心动 2021-01-02 01:28

I have a requirement to have very low latency for client to server messages in my web application.

I have seen several posts on stackoverflow saying it would be pref

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-02 01:59

    very low latency for client to server messages in my web application.

    I read this as that you want to "connect" and then send low latency messages between client and server.

    Both HTTP/2 and Websocket can be binary and the frames that your messages is transported in has similar overhead (a few bytes), but Websocket has to iterate over the full message to mask the payload and then the receiver has to reverse this. See What is the mask in a WebSocket frame?

    In addition Websocket primitives is more low-level e.g. to have multiple message streams, you have to do that on your own, but it is easily done with HTTP/2. Ref Podcast about HTTP/2. Also server code gets more complicated when using both Websocket and HTTP at the same application.

    Flow Control may be an issue using HTTP/1.1 Websocket, but is built-in protocol feature in HTTP/2.

    Server to Client

    This can be done efficiently with HTTP/2 by using fetch and response.body.getReader(); to get a ReadableStream. A good article about streams in browser JavaScript: 2016 - the year of web streams.

    Client to Server

    The only way to send messages from clients to server in HTTP currently is by sending a full request. Streamed request body is planned but not yet implemented by the browsers. See Fetch with ReadableStream as Request Body about streamed request body.

提交回复
热议问题