Does Socket.io garantee that broadcasted events ar

2019-04-22 16:56发布

问题:

Is there some kind of ordering mechanism in Socket.io that garanties that events are received in order by clients. Exemple : if Server emits event Evt1 to client A, Server broadcasts Evt2 to all clients. thud client A receives Evt1 then Evt2 and only in that order. I am a bit lazy reading the code and can't find my answer on the net. My guess is NO, and if it's the case how would you implement it or is there existing solutions?

回答1:

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.



回答2:

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:

  • https://github.com/josephg/ShareJS/issues/375
  • Can websocket messages arrive out-of-order?