[Libwebsockets] WebSocket messages: when are they atomic?

"Andy Green (林安廸)" andy at warmcat.com
Tue May 7 03:05:26 CEST 2013


On 07/05/13 08:57, the mail apparently from Daniel Griscom included:
> I'm getting up to speed on libwebsockets and WebSockets, to be used for
> communication between a browser and the web host (vanilla installation:
> no extensions). I'm designing the protocol which will be used across the
> WebSocket, and it would be great if the messages sent back and forth
> were atomic.
>
> I see that the struct libwebsocket_protocols has a rx_buffer_size; if I
> set this to the largest message that will be sent by a client then will
> each client message arrive in a single callback?
>
> And, in the other direction, how will libwebsocket_write() handle large
> messages? Can I make sure that messages below some size limit will
> arrive in a single JavaScript onmessage call?

With websockets even massive messages can be logically atomic, and its 
the websocket message as a unit that gets given to Javascript.

However it doesn't mean it's one network packet or needs to exist in 
memory all at one time at the sender (or the receiver, if it's not a 
browser).

Normally for small messages you just send a buffer in memory in one go 
and it is done.  One part of the message goes out with its FIN bit set 
indicating it's also the last part of the message.

If the messages start to get large compared to the buffer for a network 
packet, you need to cut the message into several parts.  Only the last 
part has its FIN bit set and after the initial message, the other parts 
have a CONTINUATION opcode.

You can deal with multi-part messages all in the libwebsocket_write() 
call if you're the sender.  Have a look at the test-fraggle.c example, 
for LWS_WRITE_NO_FIN and LWS_WRITE_CONTINUATION.

-Andy





More information about the Libwebsockets mailing list