Does Socket.io garantee that broadcasted events are received in order client side?
Asked Answered
C

2

20

Is there some kind of ordering mechanism in Socket.IO that guarantees that events are received in order by clients?

For example: if a server emits event Evt1 to client A, and the server broadcasts Evt2 to all clients. Thus client A receives Evt1 then Evt2 and only in that order.

My guess is NO and, if it's the case, how would you implement it, or are there existing solutions?

Columbine answered 14/3, 2013 at 13:11 Comment(0)
N
-2

No, you have to do that at the application level if you need to do that. The internet doesn't guarantee that two different packets will take the same route, so timing can vary. Maybe add a timestamp to each message so you can sort by that timestamp to keep things in order.

Nepheline answered 14/3, 2013 at 20:33 Comment(5)
Thanks! And do you know if there is some kind of npm module that uses socket.io to sync emited and broacasted messages ? Because it seems a redundant problem, for example a chat application to make sure you receive all the messages and in the right order from different people...Columbine
Just add a date field. I don't know of any libraries nor do I think one is necessaryNepheline
Hi @Max, it is necessary since you might compare two messages A < B by timestamp, but you are not sure if there is some third middle-message C (A < B < C) that you should wait for before delivering the B message. However, I hardly can imagine if A, B, C messages comes from the same client that any other client will receive them in different order keeping in mind that socket.io uses TCPScrooge
@Scrooge Instead of timestamps, you could choose for an incrementing id. When you get a message, determine if the id is one higher then the previous one. If more, then more packets are underway. So keep the current around, and handle it when all packets arrive.Baillie
I am not sure but i think you are wrong.. socket io relies on websockets and websockets rely on tcp that guarantees that packets will be received in the samr order they are sent.Condensable
T
47

Since previous answers could potentially be misleading, I think it's important to clarify one thing:

When using Socket.io with WebSockets, packet order is guaranteed to be maintained.

This is an old post, but it's worth noting that Socket.io over WebSockets actually does guarantee event ordering is maintained. This is because TCP itself, which is the underlying technology for WebSockets & HTTP guarantees packet ordering to be maintained. However Socket.io also supports several other protocols that do not guarantee order.

There have been several posted questions about this issue, supporting this fact:

Telephony answered 13/1, 2017 at 18:47 Comment(0)
N
-2

No, you have to do that at the application level if you need to do that. The internet doesn't guarantee that two different packets will take the same route, so timing can vary. Maybe add a timestamp to each message so you can sort by that timestamp to keep things in order.

Nepheline answered 14/3, 2013 at 20:33 Comment(5)
Thanks! And do you know if there is some kind of npm module that uses socket.io to sync emited and broacasted messages ? Because it seems a redundant problem, for example a chat application to make sure you receive all the messages and in the right order from different people...Columbine
Just add a date field. I don't know of any libraries nor do I think one is necessaryNepheline
Hi @Max, it is necessary since you might compare two messages A < B by timestamp, but you are not sure if there is some third middle-message C (A < B < C) that you should wait for before delivering the B message. However, I hardly can imagine if A, B, C messages comes from the same client that any other client will receive them in different order keeping in mind that socket.io uses TCPScrooge
@Scrooge Instead of timestamps, you could choose for an incrementing id. When you get a message, determine if the id is one higher then the previous one. If more, then more packets are underway. So keep the current around, and handle it when all packets arrive.Baillie
I am not sure but i think you are wrong.. socket io relies on websockets and websockets rely on tcp that guarantees that packets will be received in the samr order they are sent.Condensable

© 2022 - 2024 — McMap. All rights reserved.