33

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.

Kalamarico
  • 5,466
  • 22
  • 53
  • 70

7 Answers7

15

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)

Adrián Navarro
  • 503
  • 4
  • 14
  • 2
    +1 Great code, would you recommend making this sleep in between requests? – Doug Molineux Jul 31 '12 at 15:02
  • 1
    the server itself should make the `sleep` effect, so the server should wait before giving the response until one of these happens (1- new data found, 2- request timed out) – MhdSyrwan Aug 18 '13 at 20:25
6

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.

chanchal1987
  • 2,320
  • 7
  • 31
  • 64
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/

Jamie
  • 811
  • 9
  • 13
2

Never having used this technique and studying the Wikipedia article on the topic, "Long Polling" seems like the only viable solution. It sounds pretty simple to implement by infinitely looping and sleeping a script on the server. There's some actual code in the HTTP Streaming page linked to from the Wikipedia article.

Have you tried any of this and stumbled on specific problems?

deceze
  • 510,633
  • 85
  • 743
  • 889
2

Check out this demo video for implementing Long Polling ( comet ).. It might help you all

http://www.screenr.com/SNH

nitin
  • 109
  • 4
1

You can take a look at this article, it's a really good start to understand comet programming concepts.

You will find two examples on it. The first one use the iframe technique whereas the second one use a persistent connection.

dasilvj
  • 10,356
  • 2
  • 17
  • 16
0

For IIS, there's WebSync. Since you're using PHP, however, you might be better off with WebSync On-Demand. Either one will give you the server-push you're looking for, and is simple to use. Check out this question as well, which is basically what you're after.

Here's a simple example of WebSync On-Demand in action using no scripting language. Simply open in two windows, and see the publish/subscribe in action.

To publish from the server, you can use the PHP api.

Community
  • 1
  • 1
Jerod Venema
  • 44,124
  • 5
  • 66
  • 109