Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing WebSockets messages in order of receiving

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.

like image 499
Mateusz Avatar asked Jan 11 '13 21:01

Mateusz


People also ask

Do WebSocket messages arrive in order?

Short answer: No.

How do I receive incoming messages on WebSocket?

The Message event takes place usually when the server sends some data. Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired.

At what stage a WebSocket client is informed for the incoming message?

The OnMessage event is raised when a client sends data to the server. Inside this event handler, the incoming message can be transmitted to the clients, or probably select only some of them.

How do WebSockets communicate?

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server. The URL to which to connect; this should be the URL to which the WebSocket server will respond.


1 Answers

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:

  • Can websocket messages arrive out-of-order?
  • Websocket: are server data sent synchronously?
like image 104
Gábor Imre Avatar answered Sep 21 '22 04:09

Gábor Imre