[Libwebsockets] Receiving large message from WebSocket
andy at warmcat.com
andy at warmcat.com
Fri Jul 30 21:20:41 CEST 2021
On 30 July 2021 19:03:46 UTC, Ivan Kolesnikov <kiv.apple at gmail.com> wrote:
>Sometimes I am sending from the server to the client large messages via
>WebSocket - up to 16 KB. The problem is that the client is receiving a
>message by 1 KB chunks, so I am getting invalid messages. I tried to set a
>large rx_buffer_size for my protocol (e. g. 64 KB), but nothing changed.
There are a few things mixed together there.
1) you can set the rx buffer size per connection in the struct lws_protocols, and it's limited by the pt_serv_buf set at context creation time, defaults are 4KB IIRC
2) one logical ws 'message' will go out as many tcp packets, lws approach is to pass over the pieces as it gets them, limited by buffers in 1). Tls can affect this depending on how it's split into records which are collected until complete and signature-checked before being handed over to lws.
3) permessage-deflate also affects it, since if compressible, only a small amount of data may go on the wire to get your whole 16KB back at the peer.
It sounds like you need to look at your protocol struct rx buf limit.
>Is it possible to allow the WebSocket client to buffer the whole message
>before passing it to my callback? The client has only one connection to the
>server at one time and is not supposed to be launched on embedded
>platforms, so it is fine to have buffers with size of hundreds of kilobytes.
No lws also needs to work in embedded where linear allocations are not possible, or,eg, as ws allows, the stream is literally endless. So if you want to regenerate atomic, linear messages you can do it user side.
>Or the other way - how to determine in the protocol callback when a message
>started and finished to assemble it from chunks on my own. For now I am
>handling LWS_CALLBACK_CLIENT_RECEIVE and just take in and len arguments.
Take a look here
These apis take account of permessage-deflate too.
>Also I am using the permessage-deflate extension to reduce network load.
>Maybe it affects something.
More information about the Libwebsockets