Can websocket messages arrive out-of-order?

HtmlNetworkingWebsocketsocket.io

Html Problem Overview


If we send two messages over the same html5 websocket a split millisecond apart from each other,

Is it theoretically possible for the messages to arrive in a different order than they were sent?

Html Solutions


Solution 1 - Html

Short answer: No.

Long answer:

WebSocket runs over TCP, so on that level @EJP 's answer applies. WebSocket can be "intercepted" by intermediaries (like WS proxies): those are allowed to reorder WebSocket control frames (i.e. WS pings/pongs), but not message frames when no WebSocket extension is in place. If there is a neogiated extension in place that in principle allows reordering, then an intermediary may only do so if it understands the extension and the reordering rules that apply.

Solution 2 - Html

It's not possible for them to arrive in your application out of order. Anything can happen on the network, but TCP will only present you the bytes in the order they were sent.

Solution 3 - Html

At the network layer TCP is suppose to guarantee that messages arrive in order. At the application layer, errors can occur in the code and cause your messages to be out of order in the logic of your code. It could be the network stack your application is using or your application code itself.

If you asked me, can my Node.js application guarantee sending and receiving messages in order? I'm going to have to say no. I've run websocket applications connected to WiFi under high latency and low signal. It causes very strange behavior as if packets are dropped and messages are out of sequence.

This article is a good read https://samsaffron.com/archive/2015/12/29/websockets-caution-required

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex LopatinView Question on Stackoverflow
Solution 1 - HtmloberstetView Answer on Stackoverflow
Solution 2 - Htmluser207421View Answer on Stackoverflow
Solution 3 - HtmlmanitView Answer on Stackoverflow