Can websocket messages arrive out-of-order?
Asked Answered
K

3

89

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?

Keishakeisling answered 3/8, 2012 at 23:55 Comment(1)
#14287724Painting
K
92

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.

K2 answered 4/8, 2012 at 14:49 Comment(0)
S
32

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.

Scutter answered 4/8, 2012 at 4:3 Comment(5)
FWIW I'm definitely seeing badly out-of-order behavior in FF dev edition 45.3.0. I watched the network stream and my "1", "2", "3", "4"... etc are all in order on the wire, but the JS websocket's .onmessage() function gets called everywhichway. (simply appending the messages to document body in the callback: 3 2 1 6 5 4 9 8 7...) Don't trust the message order.Novick
@Novick That's a node.js problem, i.e. an application-layer problem, not a WebSocket or TCP problem. Messages cannot arrive in the application out of order. Application code can of course mangle the order any way it likes.Scutter
And I note that node.js is not mentioned in the question.Scutter
@Marquis of Lorne - On the contrary, node.js is absolutely mentioned in the question. "socket.io" is one of the question's tags and socket.io is based on top of node.js--there is no socket.io in operation without node.js.Urge
@Novick - socket.io supports other transport mechanisms under the hood, like polling, etc, which open multiple connections at the same time and can cause race conditions. If you didn't force socket.io to be in websocket-only mode, you can't rely on what you were seeing. Others have reported that this is reliable when socket.io is in websocket-only mode. Alternatively, using ws instead of socket.io is reported as reliable for ensuring message order.Urge
P
2

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

Perfunctory answered 15/8, 2018 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.