4

I have seen this question before and found this example in http://www.zeitoun.net/articles/comet_and_php/start which is really great and clear. However, it uses javascript.

My question is, are there any plugins, functions or something that would help me to implement PHP comet with jQuery easily? Because given example requires lots of javascript code.

And by the way, I want to use it on Apache. Is it possible?

good_evening
  • 19,773
  • 60
  • 178
  • 288
  • Apache's not designed to make using Comet very easy. See [this question](http://stackoverflow.com/questions/603201/using-comet-with-php) for more details. – justkt Apr 25 '11 at 14:37
  • @justkt Apache does in fact have comet support, see [NIO](http://tomcat.apache.org/tomcat-7.0-doc/aio.html). But I will agree it is not very easy. – Andrew Apr 25 '11 at 15:16
  • 1
    you might want to pick an answer, or you just might have lost 50 rep for nothing ^_^ – Naftali aka Neal May 06 '11 at 16:04

4 Answers4

3

Comet is long-polling where client sends a request and waits for the response from the server. The server queues the request and once it gets the updated results. It sends the response to the client.

So basically all you need to do is to send an .ajax request to the server and use the onSuccess callback to deal with the returning the data. The onSuccess callback will not be called unless the server gets the updated data.

Nothing really fancy at the client-side. The actual game is on the server side to queue the requests and then respond accordingly.

Take a look at this answer detailed code sample > How do I implement basic "Long Polling"?

Community
  • 1
  • 1
neebz
  • 10,993
  • 6
  • 44
  • 61
3

If you are only doing long polling then jQuery will work fine. However, jQuery does not expose a readyState === 3 event, so there is no built in way to get data as it is streaming if that is the direction you want to go.

[Edit] Here is the bug, #1172

And it looks like they added the functionality in 1.5, using a Prefilter

So yes, you can do all the comet stuff with jQuery now :)

Andrew
  • 13,367
  • 12
  • 62
  • 79
3

i have made the jQuery version of comet before, this is what i had done:

var comet = {
    connection   : false,
    iframediv    : false,

    initialize: function(){
        // For other browser (Firefox...)
        comet.connection = $('<iframe>');
        comet.connection.attr('id', 'comet_iframe');
        comet.connection.css( {
          left       : "-100px",
          top        : "-100px",
          height     : "1px",
          width      : "1px",
          visibility : "hidden",
          display    : 'none'
        })
        //comet.iframediv = $('<iframe>');
        comet.connection.attr('src', 'backend.php');
        //comet.connection.append(comet.iframediv);
        $('body').append(comet.connection);
    },
    // this function will be called from backend.php
    printServerTime: function (time) {
      console.log('time',time);
      $('#content').html(time);
    },

    onUnload: function() {
      if (comet.connection) {
        comet.connection = false; // release the iframe to prevent problems with IE when reloading the page
      }
    }
  }
  $(window).load(comet.initialize)
           .unload(comet.onUnload);

i had taken the code right off that page and made it jquery ^_^

Naftali aka Neal
  • 138,754
  • 36
  • 231
  • 295
1

there is a plugin i have seen, try this? http://code.google.com/p/jquerycomet/

zudokod
  • 3,984
  • 2
  • 19
  • 23