Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

php - Difference between a normal ajax and long polling

I'm trying to understand more about long polling to "manipulate" a website in real time, saw some videos and I'm thinking so far:

Say I have an old date that the sql and I make an echo on it. As long polling will know if the old date will not be the same as it will look from time to time according to the setInterval function ...?

Say I want to show publication of a blog in which all text is in mysql, but repende I publish a new publication, and who is on the page at the time, you will see the publication time (not tell me?), Then how one long polling code will know the difference between the old and the new publication? Ate even not to give conflicting or repeating the same date engraved on the sql.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since your initial question was what the difference between the two techniques is, I will start with this:

AJAX polling

Using AJAX polling to update a page will mean, that you send a request in a defined interval to the server, which would look like this:

AJAX polling

The client sends a request to the server and the server responses immediately.

A simple example (using jQuery) would look like this:

setInterval(function(){
    $('#myCurrentMoney').load('getCurrentMoney.php');
}, 30000);

The problem with this is, that this will cause a lot of useless requests since there won't be always new things on every request.

AJAX long polling

Using AJAX long polling will mean, that the client sends a request to the server and the server waits for new data to be available before he responds. This would look like this:

AJAX long polling

The client sends a request and the server responds "irregularly". As soon as the server responds, the client will send a new request to the server.

The client side would look like this:

refresh = function() {
    $('#myCurrentMoney').load('getCurrentMoney.php',function(){
        refresh();
    });
}

$(function(){
    refresh();
});

What this will do is just load the getCurrentMoney.php's output into the current money element and as soon as there is a callback, start a new request.

On the server side you usually use a loop. To solve your question how the server will know, which are new publications: either you pass the timestamp of the newest to the client available publication to the server or you use the time of the "long polling start" as indiactor:

<?
$time = time();

while ($newestPost <= $time) {
    // note that this will not count as execution time on linux and you won't run into the 30 seconds timeout - if you wan't to be save you can use a for loop instead of the while
    sleep(10000);
    // getLatestPostTimestamp() should do a SELECT in your DB and get the timestamp of the latest post
    $newestPost = getLatestPostTimestamp();
}

// output whatever you wan't to give back to the client
echo "There are new posts available";

Here we won't have "useless" requests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...