views:

89

answers:

4

I am designing a web based chat feature for an application that will work sort of like facebook chat or google talk inside of gmail. I'm wondering if anyone has any advice on how often it should check for updates? Every 2 seconds? Every 5 seconds? 10 seconds?

Also, should I call setTimeout in the callback function of setTimeout, or in the callback of the ajax request for updates? Basically, should it be n seconds between calls, or n seconds between the completion of the last request and start of the next?

Thanks

A: 

However often it updates.

What this means is that you can profile your own system, and determine the optimal time to check for updates vs load.

Noon Silk
+3  A: 

I would make it dynamic. Determine some range that you don't want to go out of, like 2 to 10 seconds, and start at some mid point, say 5 seconds. If your polls are always returning new messages, decrease the wait period toward your minimum. If polls start returning empty, increase toward your maximum. This will give you a fair balance between responsiveness when needed and giving your server a break when nothing much is going on.

Justin Johnson
+4  A: 

You could use something like Comet (AKA reverse AJAX) to have a persistent connection instead of pinging the server multiple times.

otherwise, I would say 2-3 seconds should be a good range. anything lower would be putting undue stress on the server, and anything longer would make the app feel sluggish.

GSto
A: 

Have you looked into an event based approach? That way you will get real-time behavior, save resources and save you the need for coming up with a smart polling interval.

If for whatever reason you need to use polling I would go with a short start-interval which is about double the length of time that you expect a notification to occur on average and then increase the interval from time to time if there was nothing new to avoid hammering your server unnecessarily. Though in general a good answer really depends on what your system does.

For chat, event based is the ideal way to go imho.

tosh