81

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?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Alex Lopatin
  • 965
  • 2
  • 7
  • 8
  • 1
    http://stackoverflow.com/questions/14287224/processing-websockets-messages-in-order-of-receiving – muhmud Aug 11 '14 at 11:03

3 Answers3

85

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.

oberstet
  • 21,353
  • 10
  • 64
  • 97
29

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 2
    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. – BadZen Oct 25 '16 at 20:08
  • 3
    @BadZen 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. – user207421 Mar 20 '17 at 00:06
  • And I note that node.js is not mentioned in the question. – user207421 Jun 07 '17 at 00:27
  • 2
    @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. – OCDev Nov 15 '20 at 20:27
  • 1
    @BadZen - 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. – OCDev Nov 15 '20 at 20:43
1

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

manit
  • 604
  • 7
  • 11