tags:

views:

1928

answers:

3

I have read some posts about this topic and the answers are comet, reverse ajax, http streaming, server push, etc.

http://stackoverflow.com/questions/988082/how-does-incoming-mail-notification-on-gmail-works http://stackoverflow.com/questions/732705/how-is-gmail-chat-able-to-make-ajax-requests-without-client-interaction

I would like to know if there are any code references that I can follow to write a very simple example. Many posts or websites just talk about the technology. It is hard to find a complete sample code. Also, it seems many methods can be used to implement the comet, e.g. Hidden IFrame, XMLHttpRequest. In my opinion, using XMLHttpRequest is a better choice. What do you think of the pros and cons of different methods? Which one does Gmail use?

I know it needs to do it both in server side and client side. Is there any PHP and Javascript sample code?

A: 

Try this

Just with a quick search on google for "ajax php examples"

Anyway, there isn't one right approach. It depends on what you are doing or what you want to accomplish, obviously.

klez
+23  A: 

The way facebook does this is pretty interesting.

A common method of doing such notifications is to poll a script on the server (using ajax) on a given interval (perhaps every few seconds), to check if something has happened. However, this can be pretty network intensive, and you often make pointless requests, because nothing has happened.

The way facebook does it is using the comet approach, rather than polling on an interval, as soon as one poll completes, it issues another one. However, each request to the script on the server has an extremely long timeout, and the server only responds to the request once something has happened. You can see this happening if you bring up Firebug's Console tab while on facebook, with requests to a script possibly taking minutes. It is quite ingenious really, since this method cuts down immediately on both the number of requests, and how often you have to send them. You effectively now have an event framework that allows the server to 'fire' events.

Behind this, in terms of the actual content returned from those polls, it's a JSON response, with what appears to be a list of events, and info about them. It's minified though, so is a bit hard to read.

In terms of the actual technology, ajax is the way to go here, because you can control request timeouts, and many other things. I'd reccommend (Stack overflow cliche here) using jQuery to do the ajax, it'll take a lot of the cross-compability problems away. In terms of php, you could simply poll an event log database table in your php script, and only return to the client when something happens? There are, I expect, many ways of implementing this.

Implementing:

Server Side:

There appear to be a few implementations of comet libraries in php, but to be honest, it really is very simple, something perhaps like the following pseudocode:

while(!has_event_happened()) {
   sleep(5);
}

echo json_encode(get_events());
  • The has_event_happened function would just check if anything had happened in an events table or something, and then the get_events function would return a list of the new rows in the table? Depends on the context of the problem really.

  • Don't forget to change your php max execution time, otherwise it will timeout early!

Client Side:

Take a look at the jQuery plugin for doing Comet interaction:

That said, the plugin seems to add a fair bit of complexity, it really is very simple on the client, perhaps (with jquery) something like:

function doPoll() {
   $.get("events.php", {}, function(result) {
      $.each(result.events, function(event) { //iterate over the events
          //do something with your event
      });
      doPoll(); 
      //this effectively causes the poll to run again as
      //soon as the response comes back
   }, 'json'); 
}

$(document).ready(function() {
    $.ajaxSetup({
       timeout: 60 //set a global ajax timeout of a minute
    });
    doPoll(); // do the first poll
});

The whole thing depends a lot on how your existing architecture is put together.

Kazar
It's a very nice and detailed explanation. Thank you.Do you have any sample code for one of the many ways to implement that?
Billy
Take a look at the edits just added, some sample source code, may help.
Kazar
Morgan Cheng
I think labelling PHP as a language/platform that does not scale well is not necessarily true. It can be used to develop extremely large scale systems. Look at facebook. If the developer does it right, then it will scale, if not, then it won't. Using a specific web platform isn't a guarantee of scalability. Oh, and also, the question did ask for PHP.
Kazar
A: 

I did not get it actually. Or i should say m in hurry to complete project so no time for reading stuff. i need a sample code

bluepicaso