Jump to content

  • Log In with Google      Sign In   
  • Create Account


Dealing with websocket message overload on client


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Paulus_newbieus   Members   -  Reputation: 131

Like
0Likes
Like

Posted 18 August 2012 - 03:21 AM

I have a web-socket server sending object positions to all clients at a fixed rate (say 40 Hz).
Client side (HTML5 browsers) I receive these messages and draw the object (canvas) at that new position.

My problem is that sometimes, if there is not a great deal of other JavaScript processing activity on the client, the objects seem to skip a bunch of updates and get transported over a large range. So instead of a nice smooth animation, I get stuttery movements.

I have some code to handle lagging updates using linear interpolation - but this problem is different, this is not where the updates are coming in too slow, its where they are coming in too fast!

How the heck do I solve this without resorting to decreasing the server update rate, which I really don't want to have to do?!

Any suggestions greatly appreciated!

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 14679

Like
1Likes
Like

Posted 18 August 2012 - 04:50 PM

It sounds like you are expecting the packets to arrive at the same time interval in which you sent them; this is not guaranteed. You may be experiencing Nagle's algorithm, TCP delayed-ACK, or possibly both. Turning these off is possible but it depends on the specific websockets API you're using I would imagine.

#3 hplus0603   Moderators   -  Reputation: 5110

Like
0Likes
Like

Posted 18 August 2012 - 04:57 PM

Which websockets library are you using? Anything from socket.io to cowboy can be made to do the right thing...

Also, you should timestamp each update with the step number it was valid for. This will let the client do the appropriate interpolation or extrapolation when data comes in.

enum Bool { True, False, FileNotFound };

#4 Paulus_newbieus   Members   -  Reputation: 131

Like
0Likes
Like

Posted 19 August 2012 - 04:14 AM

Aaah! Of course, why not, this sounds extremely likely. I must admit I was expecting them to arrive in the correct order. Running this simulation locally (debugging on my PC) with server local) I do not get this issue, so I did not consider the ordering to be different.

I am using Fleck:
https://github.com/statianzo/Fleck

I am just using it in its default set-up - I have not done any tweaking or tuning whatsoever, just running it out the box.

#5 ApochPiQ   Moderators   -  Reputation: 14679

Like
0Likes
Like

Posted 19 August 2012 - 04:48 AM

You should get the messages in order - just not necessarily spaced out in time exactly the way you sent them.

For example, you could send 4 messages at the rate of once every 25 milliseconds, but due to network congestion, all 4 arrive in the same "burst." This is something that can be slightly alleviated by disabling Nagle and turning off delayed-ACK, but you still need to handle burstiness correctly. As hplus suggested, timestamping your packets is the way to go here.

#6 Paulus_newbieus   Members   -  Reputation: 131

Like
0Likes
Like

Posted 19 August 2012 - 08:58 AM

OK, interesting. I cant see any obvious way to disable Nagle (never heard of it) or how to turn off delayed-ACK. I don't suppose you guys could point me to a good source of on-line explanation for these things?

I do broadcast a single message to all my client connections once every 25 milliseconds, and I do actually already attach a time stamp to the message - I use this to work out the rate to animate (linear interpolation). So even though I have the time stamp, It's not yet clear to me how I can use this to re-arrange my packets. At the moment, positions are just drawn straight away. Should I be buffering the updates, re-arrange them depending on time stamps, and then draw them? This feels like yet more lag time.

Appreciate the help guys!

#7 hplus0603   Moderators   -  Reputation: 5110

Like
1Likes
Like

Posted 19 August 2012 - 10:37 AM

To disable Nagle, you turn on the TCP_NODELAY socket option. You'll have to look at the implementation of your websocket library to see if it's already doing that. However, that's not the main problem -- even with that turned off, you'll still have variable transmission latency, and packet bunching, over the internet. Timestamping each message, and taking that into account on receipt, is the robust way of dealing with variable-latency transmission.

I don't think the packets will re-order themselves. However, they will jitter. This is why you need to introduce artificial latency on the client side, to allow for de-jitter in presentation.
enum Bool { True, False, FileNotFound };

#8 Paulus_newbieus   Members   -  Reputation: 131

Like
0Likes
Like

Posted 20 August 2012 - 03:32 PM

Thanks for the help guys. You were correct - by turning off the Nagle algorithm, my jitter completely vanished!

For interest of other readers, in .Net this is possible by toggling the NoDelay property to true (by default its false) on the System.Net.Sockets.Socket class.

I believe this change will soon be integrated into the Fleck library by the project developer.

Cheers

Edited by Paulus_newbieus, 20 August 2012 - 03:33 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS