Comet with Nginx and jQuery

This is an introduction into a basic Comet setup with
Nginx and jQuery.
You will have to recompile Nginx with
NGiNX_HTTP_Push_Module
to enable the HTTP Push/Comet functionality. “NHPM” is based on the
Basic HTTP Push Relay Protocol and turns Nginx into a very efficient
Comet server. This simple recipe will enable you to create live
asynchronous web applications utilizing long polling without the
complexity of the Bayeux protocol.

First you will need to recompile Nginx with NHPM. If you run into
dependency problems please review the Nginx Install Options.

Next we will configure a virtual host in Nginx to enable our publisher
and subscriber on a single channel called “cheetah”. This configuration
allows the queue to hold up to 10 items, and those items will timeout
after 5 seconds.

Start nginx:

sudo/usr/local/nginx-0.7.67/sbin/nginx

Here is the code for the subscriber that will listen for incoming messages
and simply display them. Notice the the use of “If-None-Match” and
“If-Modified-Since”. These headers are used to traverse the messages
in the queue. The ETAG header is used to uniquely identify messages
with the same modification time.