[Libwebsockets] WebSocket messages: when are they atomic?

Daniel Griscom griscom at suitable.com
Sat May 11 18:45:50 CEST 2013


Let me make sure I understand. In my application, 
I don't care much about either generating or 
receiving partial blocks in order to save memory, 
because my messages won't be that large (perhaps 
a few kB max). I just want to be absolutely sure 
that the messages I send (either way) are 
interpreted atomically.

So, I have four cases to handle:

1) Sending from JavaScript in browser: just use 
the WebSocket instance's send() method. The whole 
text will be sent as a single message.

2) Receiving in JavaScript in browser: each 
atomic message will be handed to the WebSocket's 
onmessage() handler as a single string

3) Sending from libwebsockets in server: use 
libwebsocket_write() to send the whole message 
(but what if not all characters are sent? see 
below...)

4) Receiving in libwebsockets in server: use 
protocol callback routine when reason == 
LWS_CALLBACK_RECEIVE, calling 
libwebsockets_remaining_packet_payload() to make 
sure there aren't more characters coming in the 
message


Did I get them all correctly?

And, in case number 3, when might 
libwebsocket_write() not write all the 
characters? Only if the application as a whole is 
running out of memory? And, if it does bail 
early, are the sent characters packaged as a 
single message, or may I append more characters 
to the message with an additional call?


Thanks,
Dan


At 9:05 AM +0800 5/7/13, Andy Green (ó-à¿úN) wrote:
>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


-- 
Daniel T. Griscom             griscom at suitable.com
Suitable Systems              http://www.suitable.com/
1 Centre Street, Suite 204    (781) 665-0053
Wakefield, MA  01880-2400



More information about the Libwebsockets mailing list