Simple comet example using php and jquery

Can anyone give me a good and simple example of the comet technique using PHP?

I just need an example that uses a persistent HTTP connection or something similar. I don't want to use a polling technique, because I have something like that set up and not only is it difficult to work with and manage its a big hog of resources. Also I am using IIS7 not Apache.

A good example would be really helpful so I can move on from this ugly polling technique.


Solution 1:

You should use polling, or use a web server which is specially conceived for long requests and COMET, with a good JS backend:

function listen() {
    $.get("/mylongrequestfile", {}, function(data) {
        $("#mydiv").html(data);
        listen(); // then launch again
    }));
};

Remember that COMET is "wait for data, if there's data return and exit", so JS backend will have to parse the data and re-launch the process of asking the server.

In this example, if there is a server side problem or just a disconnection from the user side, the entire process will be broken (the function is only called if the request is successful)

Solution 2:

Check this out: How to implement COMET with PHP.
This is not using JQuery. It is made using PHP and Prototype. It is very easy to understand. I think you can made JQuery script easily after viewing this.

Solution 3:

I have a very simple example here that can get you started with comet. It covers compiling Nginx with the NHPM module and includes code for simple publisher/subscriber roles in jQuery, PHP, and Bash.

http://blog.jamieisaacs.com/2010/08/27/comet-with-nginx-and-jquery/

A working example (simple chat) can be found here:
http://cheetah.jamieisaacs.com/