views:

241

answers:

3

Hi all, I am wondering if anyone could show, or explain with examples, how facebook checks its database for new messages? It seems to do it periodically, without loading the page. What would be the best way to achieve this in a PHP/MySQL/Jquery script?

alt text

Any help is always appreciated! Cheers, Lea

A: 

There is actually a "page load", but it's a hidden request that doesn't reload the displayed page. Take a look at the jQuery Ajax command documentation for more details on one of the simplest ways to accomplish this (especially since you already mentioned using jQuery).

Amber
+3  A: 

you can do this: usign periodical updater

<span id="inbox-title"></span>


<script>

$.PeriodicalUpdater('/path/to/service', {
  method: 'get',          // method; get or post
  minTimeout: 1000,       // starting value for the timeout in milliseconds
  maxTimeout: 8000,       // maximum length of time between requests

}, function(data) {
   $('#inbox-title').html('you have ' + data + 'new messages');

});


</script>

another option is to bind the onmousemove event and make the ajax call when than happes

Gabriel Sosa
`onmousemove` really isn't ideal for something that's designed to be updated on a regular, timed basis.
Amber
Thank you gabriel!
Lea
A: 

Have a look into reverse ajax with the COMET technique, this is a perfect use for it.

The idea behind it is to start an ajax request and let it timeout which could be 60 seconds, when it times out, start it again, here the browser has a (nearly) persistent connection to the server, if (for a simple example) a message gets created for a user. the server can reply to one of the hanging ajax requests that have been made (in this case by the recipient of the message).

No data is transfered while the xmlhttprequest and the server are waiting, but closing and reopening connections might be a burden on your server.

Question Mark