Processing WebSockets messages in order of receiving
Asked Answered
T

2

15

Client side part of my application needs to process WebSocket messages in strict order. Unfortunately each message is processed quite long (about 3 seconds), so another appears before the first one has ended. After few messages the order is totally different. How to solve this problem in JavaScript.

I thought about a task queue, but I don't know how to implement it to not block GUI of my web app.

Tondatone answered 11/1, 2013 at 21:50 Comment(2)
Doesn't this contradict #11805221 ?Barely
See also https://mcmap.net/q/236395/-can-websocket-messages-arrive-out-of-orderOliveira
D
-7

At this time, the WebSocket API in browsers does not expose a frame based or streaming option/API. So there is not really an option to accomplish this natively.

That means you need to create your own logic / structure within the data packets you're sending from your server to clients. That should be a relatively easy task, I could think of transmitting simpl JSON strings/objects which might have a indexed property on top level.

{
    packetIndex: 0,
    data: { }
}

and your client script would need to look into each arriving packet and sort things out correctly (keeping track of arrived packets and "hold + wait" if a packet with a too high indexed arrived)

Dolly answered 11/1, 2013 at 21:57 Comment(2)
That seems wrong "Message fragments MUST be delivered to the recipient in the order sent by the sender."Decipher
This is indeed the correct answer, TCP sends the packet in the correct order, the client receives it in the correct order, but client might take time to process each request, so when it take the next one, it might process the wrong one. For example, in Golang, I launch them in go routines to increase performance.Didymium
S
29

I think the other answer is wrong. WebSocket IS TCP, which means that the order of delivery is guaranteed. As @Maël Nison quoted, see RFC6455:

Message fragments MUST be delivered to the recipient in the order sent by the sender

So you can take it granted that your processing will start in order. However, if you have lots of async callbacks then a later processing may finish before an earlier is still going on. But that's simply wrong implementation (and a bit of callback hell).

Similar posts:

Siddur answered 8/8, 2015 at 14:30 Comment(2)
I've made some stress tests with SocketIO, EngineIO and ws libraries. Something was wrong with the message orders in EngineIO (almost same code as for SocketIO; I didn't dig to the depths), but message order was fine in SocketIO and ws -- even when the server was half-dying under the stress test.Majuscule
What if you have two messages sent simultaneously (Or as close to it as two async methods may cause). I get that the frames in message A will arrive in order, but will message B wait for message A's frames to finish arriving before polluting my receive buffer? receiveBuffer{ByteFromA, ByteFromA, ByteFromB, ByteFromB, ByteFromA} could cause havock when deserializing.Fiasco
D
-7

At this time, the WebSocket API in browsers does not expose a frame based or streaming option/API. So there is not really an option to accomplish this natively.

That means you need to create your own logic / structure within the data packets you're sending from your server to clients. That should be a relatively easy task, I could think of transmitting simpl JSON strings/objects which might have a indexed property on top level.

{
    packetIndex: 0,
    data: { }
}

and your client script would need to look into each arriving packet and sort things out correctly (keeping track of arrived packets and "hold + wait" if a packet with a too high indexed arrived)

Dolly answered 11/1, 2013 at 21:57 Comment(2)
That seems wrong "Message fragments MUST be delivered to the recipient in the order sent by the sender."Decipher
This is indeed the correct answer, TCP sends the packet in the correct order, the client receives it in the correct order, but client might take time to process each request, so when it take the next one, it might process the wrong one. For example, in Golang, I launch them in go routines to increase performance.Didymium

© 2022 - 2024 — McMap. All rights reserved.