Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server polling intervals for a javascript chat client

I'm building a basic little AJAX shoutbox/chat for my website, but I'm not sure exactly how to implement the server polling.

Here's the basic program flow I'm thinking of:

  1. User comes to page and is shown the last 10 messages
  2. To get messages sent by others, the client javascript would request a URL with a timestamp parameter (set to the value of the last message the client received)
  3. The server returns all messages (up to a max of 10) since that timestamp.

The only issue is how often to poll the server. Obviously it should poll each time a new message is added, but when you're just reading others' messages it needs to automatically update.

Should it be a set time limit? eg: every 10 seconds. Or, should it vary depending on usage? eg: Check after 5 seconds. If there's no messages, don't check for another 10 seconds. If there's still no new messages, check in 15 seconds, then 20, up to maybe once every 30 seconds max. Each time there's a new message detected reset your timer back down to 5 seconds and start again.

I'm just concerned about putting unnecessary stress on the server, considering that we could have hundreds of users concurrently online.

...or have I got the whole thing wrong? Is there a better way to implement a basic javascript chat?

like image 788
nickf Avatar asked Mar 24 '09 03:03

nickf


1 Answers

You might want to look into what are known as Comet programming techniques to stream information down to your users, rather than having the client poll the server. This is actually a family of techniques, some of which may work better than others depending on the circumstances, such as what kind of server you're using and what kind of client compatibility you need.

If your server can handle a large number of open connections at a time (as in, it does not use an entire thread or process per connection, such as nginx or an erlang based server), you may wish to use a long polling technique, where as soon one message is received, the client immediately requests another message. If there are no messages available, the server simply keeps the connection open, possibly sending occasionally dummy data as a keepalive, until a message becomes available.

like image 136
Brian Campbell Avatar answered Nov 11 '22 22:11

Brian Campbell