-1

I'm getting my toes wet with web sockets for the first time and I am trying to convince myself (after having read through the RFC and flicking through stack overflow) that it's not possible for frames to arrive out of order, so long as they are sent in the right order in the first place.

For example, let's say I have Alice and Bob connected over a websocket. Alice sends 100 binary messages to Bob in a single-threaded vanilla for loop via Jetty's RemoteEndpoint#sendBytes method. Bob is listening on the other end and waiting for bytes via WebSocketAdapter#onWebSocketBytes and adding them in to a queue for processing.

Can Bob receive the bytes out of order? From what I've observed, it doesn't look possible - the websocket code in Jetty seems to 'lock' the connection during the onWebSocketXXXX calls and so anything out of order is more likely to be my buggy application code.

I'm going to do some more digging/experimenting but was hoping to get a steer from someone who's trod this path before on whether I'm safe to rely on the frames appearing in serial.

Thanks in advance, Nick

  • Why would frames arrive out of order, if bytes on the underlying TCP connection don't? – gre_gor Aug 25 '22 at 02:52
  • Yes, that's exactly my question - is there anything in jetty's dispatching/threading that might cause this to happen - Lachlan seems to think not, I have my answer, thanks! – Nick Griffiths Aug 28 '22 at 21:22

1 Answers1

0

The frames should be received in the order they were sent.

the websocket code in Jetty seems to 'lock' the connection during the onWebSocketXXXX calls

Its not that Jetty is locking the connection, its just that for any WebSocket connection there is only a single thread reading and parsing WebSocket frames then delivering them to the application. So the application will receive them in the same order they are parsed (which is the same order they were received over the network).

Lachlan
  • 356
  • 1
  • 7
  • That's reassuring, thanks Lachlan - I noticed a bunch of websocket client threads and couldn't see how that thread pool was being mapped to connections. I have a test that's intermittently failing with a frame out of order and I wanted to rule out Jetty. – Nick Griffiths Aug 28 '22 at 21:24