0

I´m trying to learn how long polling work. I have read many tutorials out there and found most of them use jQuery. For me using jQuery is nice because it make thing easier. But to talk about learning javascript basic. Using jQuery is not the first choice for me to think about.

I have made my own lomg polling and have some question about it.

in javascript ( I will not have whole script because I guess people understand it very basic)

function callme() {

var tmstmp= new Date().getTime(); // sending time stamp now to php
var url="test2.php?user_id=298&tmstmp="+tmstmp;
var params="user_id=298&tmstmp="+tmstmp;

xmlHttp=GetXmlHttpObject()
xmlHttp.onreadystatechange=function () {

 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){

if (xmlHttp.responseText!=""){

document.getElementById("result").innerHTML=xmlHttp.responseText;
 }

setTimeout("callme()",1000);

 }
}

In PHP

$user_id=$_POST['user_id'];
$tmstmp=$_POST['tmstmp'];
$params=$_POST['params'];

  $stmt = $conn->prepare('SELECT * FROM user where recipient=:recipient ORDER BY cr_id desc');
  $stmt->bindParam(':recipient', $user_id);
  $r=$stmt->fetch();

$currentmodif=$r['database_timestamp'];

if ($currentmodif <= $tmstmp) { ///**HERE IS THE QUESTION !!
  sleep(1);
  clearstatcache();
 $currentmodif =$r['database_timestamp'];
 }

echo $r['message'].'-->'.$tmstmp;

My question is :

Actually in php

  • if ($currentmodif <= $tmstmp) Must be While ($currentmodif <= $tmstmp)

According to the tutorials I found. But it´s not work if I use while Until I change it to be (>) WHY ?

  • If I will keep on using if like this instead of using While is it wrong to do ?

    Do you have any suggestion about this script I made ?

After a while (let´s say some minutes) I got message Service unavailable bla bla bla

I really wonder how the other poeple do the long polling without problem, Example facebook or other chat comet.

Thank you for any answer.
Poramat Fin
  • 121
  • 1
  • 10
  • the `if` and the `while` are performing different operations with similar results, thats why it is eluding you. If using `if` as you are; each second js will ping php, it will check, wait (for no reason) and return. if you are using the `while` construct, it will perform as intended, but with perhaps too much load. js will ping php, where php will wait for response before sending back to js. There js will wait a second and try again. Similar but different operations. – NappingRabbit May 14 '15 at 16:37

1 Answers1

1

Edit:

Basically my answer was to switch from long polling to short polling. This was wrong of me. I am a bad answerer.

clarification lives here, along with some pros/cons and possibly a reason for your error:
Scaling a chat app - short polling vs. long polling (AJAX, PHP)

Original:

First glance: I think the issue you're running into is that you're polling every second, but telling the server to sleep for a second. if you change that if to a while then you'll be telling it to sleep indefinitely, until a change has been made to the db and the $currentmodif is updated. What you want to do is send a response back to the js, not wait for a change to be made before responding.

instead try this logic:

  • send the timestamp through as you're doing, but if the database hasn't been updated, just return an empty response or a code you can recognize as "not updated" and do nothing.
  • get rid of that "sleep", theres no need to wait to return a response.
  • suggestion: in the javascript, use a setInterval instead of a recursive setTimeout, and keep a reference to that interval around so you can clearInterval later and stop the polling.
  • I'm 99% positive that facebook and other services use websockets instead of polling (at least for any browsers that support it). you may want to investigate this.
Community
  • 1
  • 1
nebulae
  • 2,665
  • 1
  • 19
  • 16
  • nebulae Thanks for you comment. I agree with you. But there´re some questions: if do so what you suggest me, is it not different from a normal ajax call using setInterval ? I mean my goal is : to reduce the much work for server. If I do so it can not be call long polling any more ? I´m thinking that I maybe find out to set myself into websockets as you said. Because I can see the error after I stand by the page to run in more minutes. Seems like too much call. – Poramat Fin Sep 05 '13 at 16:10
  • I could do it like every 30 second. But my goal is Fast as instant message but I do not want problem after I have lots of users. What you will say ? – Poramat Fin Sep 05 '13 at 16:11
  • setInterval and setTimeout will do pretty much the same thing, I just think its cleaner to use `var interval = setInterval('callme', 1000)` outside of the callme() method. I don't think the problem is coming from polling the server every second, I think the error is coming form telling the service to `sleep(1)` until the `$currentmodif` is updated. Sorry if I didn't make that clear. My suggestion was to get rid of the sleep and return a response to the js immediately. – nebulae Sep 05 '13 at 16:16
  • Sounds good anyway. I spent lots of time to read many tutorials. I found no real solution. Just example how it work but can not be used in real. Because lots of problem are following. I can not see any problem now but I can notice when threr´re more user, the web start being slower. Because therere lots of calling from this function I made. – Poramat Fin Sep 05 '13 at 16:21