How does WebSocket compress messages?

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)

Leave a Comment