Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websockets message integrity

When using a WebSocket full-duplex data connection between a client and a server, am I guaranteed, when sending two messages from the server, that I will receive those two exact same messages on the client, something TCP does not ?

In other words, if the server sends in turn hello and then its me, does the client always receive two messages containing hello and its me or is it possible the client receives helloit and then s me or even one only message like helloits me ?

like image 493
Magix Avatar asked Mar 11 '16 02:03

Magix


2 Answers

WebSocket originally had text message start and finish bytes, but that's old information. There's a length component in the (Dec 2011) framing. And btw, there a flag in the framing spec that specifies whether the frame is the whole "message" or a "message fragment", ie, there are other frames that make up this "message". Its up to the receiver to combine the fragments correctly to pass up to the application code. Normally you don't have to think about this unless you have very large "messages" (think of WS "messages" as "data" rather than true messages... WS is not really a "messaging" protocol per se).

But note, WebSocket is a low-level transport. Think of it like a web-based TCP. Application programmers should use high-level protocols "over WS/WSS" and not worry about ordering, reconnection, presence, pub/sub, tuple-spaces, guaranteed delivery, etc, etc. If we don't do this, dozens and dozens of decades-old application protocols will be re-invented.

like image 97
FrankG Avatar answered Sep 23 '22 15:09

FrankG


Having a very bad connection you may lose a message (however it should be resend with tcp), but it's very unlikely to be splitted in such a way. Each text message has boundaries - start (0x00) and finish (0xFF) bytes. Browser should not mess messages. Likely you can get messages in another order.

How can you be more sure:

  1. Add your own control sums to messages (but indeed it's overhead).
  2. Use wss (websockets+tls/ssl) protocol - corrupted messages would not be decrypted well.
like image 37
Eugene Lisitsky Avatar answered Sep 23 '22 15:09

Eugene Lisitsky