30

After the success handshake of WebSocket, could we used gzip compression?

Here are my tests:

  1. I use autobahn lib to build a server, then respon to client as:
    HTTP/1.1 101 Switching Protocols content-encoding: gzip Connection: Upgrade Server: AutobahnPython/?.?.? Upgrade: WebSocket Sec-WebSocket-Accept: RIR8KmljoV8Cv9mdiLY7GM2nYMc=
  2. then my server uses gzip compression
  3. and the chrome browser got the result, but it told me that "could not decode a text frame as UTF-8"
eldarerathis
  • 35,455
  • 10
  • 90
  • 93
littlesun
  • 301
  • 1
  • 3
  • 4
  • AutobahnPython (currently) does not support the per-frame compression extension. In any case, signaling of support is done via a WebSocket specific HTTP header, not "content-encoding". – oberstet Jul 25 '12 at 12:00
  • Thanks for reminding. Any way, has a simple way to support gzip compression as the http method while using websocket? – littlesun Jul 25 '12 at 12:28
  • No. For using compression on WebSocket, the extension needs to be implemented, which requires significant code "deep down under the hood". – oberstet Jul 25 '12 at 16:15
  • Any luck getting the compression to work? Im quite interested in this myself. – Dreen Jul 31 '12 at 19:11
  • AutobahnPython now supports "permessage-deflate". You can test it with Chrome Canary (currently the only browser implementing it). You'll need the permessage_deflate branch on AutobahnPython GitHub or wait for 0.6.0 - will be released soon. – oberstet Jun 07 '13 at 09:26
  • [Update in 2018] We have deflate algorithm over WS messages (not frames). More info here: https://stackoverflow.com/questions/19298651/how-does-websocket-compress-messages – Gábor Imre Dec 03 '18 at 11:45

3 Answers3

12

WebSocket compression is enabled in some browsers by default (at the time of writing for example in Chrome, but not in Firefox). The client has to include the 'Sec-WebSocket-Extensions: permessage-deflate' header for this. If the server responds with the same extension, the WebSocket communication is compressed on a frame basis. As far as I know, there is no browser API to enable/disable extensions.

A good article about the topic is: https://www.igvita.com/2013/11/27/configuring-and-optimizing-websocket-compression/

Joel
  • 15,496
  • 7
  • 52
  • 40
7

There is a compression extension being worked on by the IETF Websocket (HyBi) working group. I would suggest following their mailing list for up-to-date information. I also recommend checking out this question.


Update 2017: The extension has now been available for some time, see here: https://www.rfc-editor.org/rfc/rfc7692

Community
  • 1
  • 1
Dreen
  • 6,976
  • 11
  • 47
  • 69
  • Would you tell me how to use? I did look up in the RFC 6455, but I don't know how to use ... – littlesun Jul 25 '12 at 10:06
  • Thanks for Dreen, that is great. I will study the compression extension. Is any other friends studying on it? – littlesun Jul 25 '12 at 13:20
  • 1
    @littlesun: As far as I know, this extension is not available yet as it is still being worked on (the prime evidence of this is that they just changed it from a "per-frame" to "per-message" appliance). Javascript itself does not really support any form of compression/decompression. I'm afraid the only solution for now is wait for the IETF folks to finish the spec and for browser vendors to implement it... It shouldn't be long, IMHO it will probably happen in a couple months. – Dreen Jul 25 '12 at 14:52
  • 2
    It is no long a draft. Please refer to [RFC 7692, Compression Extensions for WebSocket](https://tools.ietf.org/html/rfc7692) for the proposed standard. – HKTonyLee Sep 25 '17 at 00:44
  • IETF has officialy defined only "permessage-deflate" as of now. As per [RFC 7692, Compression Extensions for WebSocket](https://datatracker.ietf.org/doc/html/rfc7692). permessage-gzip is not officially supported by IETF yet. You may follow [IETF mails](https://mailarchive.ietf.org/arch/search/?q=permessage-) here for the latest updates. – Ajmal Moochingal Oct 06 '21 at 15:47
0

Yes it could. Chrome 19+ supports it.

"https://github.com/crossbario/autobahn-python/blob/master/examples/twisted/websocket/echo_compressed/server_advanced.py"

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File

from autobahn.twisted.websocket import WebSocketServerFactory, \
    listenWS

from autobahn.websocket.compress import *

def accept(offers):
    for offer in offers:
        return PerMessageDeflateOfferAccept(offer)

debug = True
factory = WebSocketServerFactory(u"ws://127.0.0.1:9000", debug=debug, debugCodePaths=debug)
factory.setProtocolOptions(perMessageCompressionAccept=accept)

listenWS(factory)

webdir = File(".")
web = Site(webdir)
reactor.listenTCP(8080, web)

reactor.run()

More info: how PerMessageDeflateOffer is used in Autobahn examples.

Community
  • 1
  • 1
Cees Timmerman
  • 17,623
  • 11
  • 91
  • 124