Back when I was a student I messed around with websockets (back then it was grizzly on glassfish). Now adays most of my development is done in python. The nice thing about websockets is that it's like a TCP-socket where both parties can send data, websockets are like this, but over HTTP. This allows the server to send data to the client without the client being the active party.

The basic "usecase" for long-polling is described below.

  1. The client initiates an ajax-request to the server.
  2. The server check to see if it has something to return, if it has, it returns it and we go back to step 1.
  3. If the server had nothing to return, it waits for a period of time (in my case 20 seconds), check now and again if it has something to return. If it finds something, it returns it and we go to step 1.
  4. If the 20 seconds pass and the server still does not have anything to send the connection is closed (ie the server returns 200 OK or similar), and the client makes a new connection from step 1.

This method brings a lot of overhead, on the plus side it is supported on all browsers that can make ajax-requests reasonably well.

My implementation of long-polling is done in Django, it is focused on keeping the model clean, the javascript tight and the long-polling technique robust. I have tested it on IE 6,7,9 and 10 as well as firefox and chrome.

A running version can at the time of writing be found here:

http://wc.dhedegaard.dk/

The source can be found here:

https://bitbucket.org/dennishedegaard/webchat

I will most likely try to put it up in the cloud (ie GAE, which support Django 1.4 these days), to make sure it stays up.