长轮询 - jquery + php

I want to long poll a script on my server from within a phonegap app, to check for things like service messages, offers etc.

I'm using this technique in the js:

(function poll(){
$.ajax({
    url: "/php/notify.php",
    success: function(results){
        //do stuff here
    },
    dataType: 'json',
    complete: poll, 
    timeout: 30000,
});
})();

which will start a new poll every 5 minutes (will be stopping the polling when the app is 'paused' to avoid extra load)

I am not sure how to set up the php though? I can set it up so it doesnt return anything and just loops trough the script, but how to make it return a response as soon as i decide i want to send a message to the app? my php code so far is:

<?php

include 'message.php';

$counter = 1;

while($counter > 0){
//if the data variable exists (from the included file) then send the message back to the app
if($message != ''){
    // Break out of while loop if we have data
    break;
}
}

//if we get here weve broken out the while loop, so we have a message, but make sure
if($message != ''){
// Send data back
    print(json_encode($message));
}

?>

message.php contains a $message variable (array), which normally is blank however would contain data when i want it to. The problem is, when i update the $message var in message.php, it doesnt send a response back to the app, instead it waits until it has timed out and the poll() function starts again.

so my question is, how do i set-up the php so i can update the message on my server and it be sent out instantly to anyone polling?

Long polling is actually very resource intensive for what it achieves

The problem you have is that it's constantly opening a connection every second, which in my opinion is highly inefficient. For your situation, there are two ways to achieve what you need; the preferred way being to use web sockets (I'll explain both):


Server Sent Events

To avoid your inefficient Ajax timeout code, you may want to look into Server Sent Events, an HTML5 technology designed to handle "long-polling" for you. Here's how it works:

In JS:

var source = new EventSource("/php/notify.php");
source.onmessage=function(event) {
  document.getElementById("result").innerHTML+=event.data + "<br>";
};

In PHP:

You can send notifications & messages using the SSE API interface. I don't have any code at hand, but if you want me to create an example, I'll update this answer with it

This will cause Javascript to long-poll the endpoint (your PHP file) every second, listening for updates which have been sent by the server. Somewhat inefficient, but it works


WebSockets

Websockets are another ballgame completely, and are really great

Long-Polling & SSE's work by constantly opening new requests to the server, "listening" for any information that is generated. The problem is that this is very resource-intensive, and consequently, quite inefficient. The way around this is to open a single sustained connection called a web socket

StackOverflow, Facebook & all the other "real-time" functionality you enjoy on these services is handled with Web Sockets, and they work in exactly the same way as SSE's -- they open a connection in Javascript & listen to any updates coming from the server

Although we've never hard-coded any websocket technology, it's by far recommended you use one of the third-party socket services (for reliability & extensibility). Our favourite is Pusher