Does WebSocket provide support for data compression to save bandwidth? What are the options available? One possible solution could be use of Bijson in place of Json.
4 Answers
The current WebSockets protocol draft does not contain a compression extension. There was one formerly: deflate-stream, which works by compressing the whole WS stream. The effectiveness of that is limited, since WS introduced client-to-server frame masking, with mask changed per frame, and by that, deflate would not be able to keep a effective compression dictionary.
There is a draft proposal for frame-based compression which works around this, since the compression dictionary is maintained for the payload before masking.
Comments
The first way that Websockets save bandwidth is by leaving the connection open for multiple (bi-directional) messages. The connection can remain open as long as it's needed. This means that a new connection does not need to be negotiated for every transaction like the old http approach. The messages themselves have header information that specifies whether the incoming message is text or binary, and how long the "payload" is.
You can let your service interpret the messages in whatever way you want. Specific compression related data can be expressed through extensions: See section 9 of the standard: https://datatracker.ietf.org/doc/html/draft-ietf-hybi-thewebsocketprotocol-15#section-9
The standards organization has produced a working draft for compression extension: https://datatracker.ietf.org/doc/html/draft-tyoshino-hybi-websocket-perframe-deflate-00
Comments
You could tunnel everything through an ssh connection with encryption turned off and compression set to maximum?
e.g.:
ssh -N -g -f -C -o CompressionLevel=9 -o Cipher=none [email protected] -L 6999:172.16.1.218:3129