10

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.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Kevin mtk
  • 253
  • 1
  • 2
  • 11
  • It shouldn't be the job of the long polling javascript method to make these calculations. Your server side code should be determining these things to be honest. The javascript should only be making a conditional statement against returned data. Try not to put business logic in the client side if you can. – Ohgodwhy May 15 '15 at 05:58
  • Take a look for google search: simple chat using long polling – Kevin mtk May 15 '15 at 06:07

1 Answers1

21

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.

Marc
  • 3,683
  • 8
  • 34
  • 48
  • Oh, good answer, my friend, I could understand everything, but what if I want to make a append to the body with the new date, it is advisable? – Kevin mtk May 15 '15 at 15:20
  • So you want to append the new blog post to the body? Well, I would recommend something similar to what stackoverflow does (if you don't know what that is: go to a tag - e.g. PHP and wait for ~1 minute. There will be a box which says, that there are 'x' new questions). So don't append it because if you do this, the reader could be interrupted because you moved the whole content. A box with a message that you can click is simpler. When you click it, the new posts will be appended :-) – Marc May 15 '15 at 15:26
  • Got it, most do not understand how I will establish the parameters not to repeat the old dates recorded in mysql, it seems I'll never understand about this, but it is what I need mostn :/ – Kevin mtk May 15 '15 at 15:53
  • 3
    sleep() is written in seconds, not milliseconds. So it should be sleep(10) :) – Rasmus Oct 02 '20 at 09:41
  • will this work in a multi-instance app? will the client still send new requests to the same server after scale out? – Edwin Vivek N Sep 07 '21 at 10:47