How does WebSocket compress messages?

后端 未结 3 1378
抹茶落季
抹茶落季 2020-11-29 03:48

JSON.stringify is obviously not very space efficient. For example, [123456789,123456789] occupy 20+ bytes when it could need just around 5. Does websocket compress its JSONs

3条回答
  •  温柔的废话
    2020-11-29 04:44

    WebSocket is, at its heart, just a set of framing for TEXT or BINARY data.

    It performs no compression on its own.

    However, the WebSocket spec allows for Extensions, and there have been a variety of compression extensions in the wild (the formalized specs for one of these is finalized).

    As of today (August 2018) the accepted compression spec is permessage-deflate.

    Some of the extensions seen in the wild:

    • permessage-deflate - the name of the formalized spec for using deflate to compress entire messages, regardless of the number of websocket frames.
    • x-webkit-deflate-frame - an early proposed compression that compresses each raw websocket data frame. Seen in use by Chrome and Safari. (now deprecated in Chrome and Safari)
    • perframe-deflate - a renamed version of the above compression. Seen in use by various websocket server implementations, and also briefly showed up in various WebKit based clients. (Completely deprecated in modern browsers, but does still show up in various WebSocket client libraries)

    Of note, the permessage-deflate extension is the first in a line of PMCE (Per-Message Compression Extensions) that will eventually include other compression schemes (ones being discussed are permessage-bzip2, permessage-lz4, and permessage-snappy)

提交回复
热议问题