warmcat / libwebsockets

canonical libwebsockets.org networking library

Home Page:https://libwebsockets.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

compression

horchi opened this issue · comments

probably a stupid question but I couldn't find anything about it. As I understand it here the communication via WebSocktets is always compressed by default?
Does the libwebsockets do this automatically when sending or do I have to configure something, set an option or something similar?
Thx and best regards

RFC6455 defining websockets came out in 2011 without any compression support, the permessage-deflate extension was added in RFC7692 in 2015 . So ws itself in the RFC6455 sense has no idea about compression and cannot use it by default.

Particular implementations might try to use permessage-deflate extension by default but there's no requirement for it to exist either end. The client doesn'trequest it by default, you have to add some code, see either

../minimal-examples-lowlevel/ws-client/minimal-ws-client-pmd-bulk/minimal-ws-client-pmd-bulk.c or
../minimal-examples-lowlevel/secure-streams/minimal-secure-streams-binance/main.c

If the client requests it, it's OK if the server doesn't support it, it will just not succeed to be negotiated. If both ends can agree on it then it will be automatically applied.

Thanks, I'll take a look and give it a try. My goal is to compress the communication since a slightly larger packet that rarely needs to be transmitted to the client slows down the communication a bit on bad connections

just to make sure I have understood it correctly, if the client (e.g. the browser) requests deflate the libwebsockets handle and use this automatically?

... if the client supports it and asks for it, and the server supports it, both sides will negotiate to use compression during the normal ws upgrade.

For lws client, you need to add the boilerplate from the examples I mentioned to enable permessage-deflate extension request and then that should be enough if the server supports it.

I use libwebsockets as server. I added the permessage-deflate to the client side (javascript).
The permessage-deflate is requested by the client but not in the response as far as I can see here:
grafik

Lws needs to be built with -DLWS_WITHOUT_EXTENSIONS=0

Thx, now it looks better, it seems I need both, building the lib with -DLWS_WITHOUT_EXTENSIONS=0
and setting this on server side:

static const struct lws_extension extensions[]
{
   { "permessage-deflate", lws_extension_callback_pm_deflate, "permessage-deflate"
         "; client_no_context_takeover; client_max_window_bits" },
   { NULL, NULL, NULL }
};

grafik