0

I'm working currently on a long polling script where i've to check a database for new changes.

I'm wondering if it will be too much resources consuming to do the query in a while loop then do the query again without any delay, or if i should let a little delay like one second.

When I look on Facebook for example it seems to have the new changes within the second so i guess they they don't have any delay while checking the database or this delay is really short like half a second.

I don't expect a straight answer but more advises on the best practices for this

Thanks

Jerome Ansia
  • 6,854
  • 11
  • 53
  • 99
  • PHP is designed to be executed prior to the html, are you intending to flush() this data to the user while endlessly looping through a php script? I'm sorry, I don't understand your question. I haven't heard of any PHP long polling, because it's a pre-processor. If you want relative "real time" updates, it's much simpler via AJAX implementation. – Blake Apr 05 '12 at 15:14
  • Yes the way to do it it's to play with the page timeout and don't output anything until you get something new to output. If you want to have an example of the way it's working, go on Facebook in Chrome and when you do an inspect go into network then you'll see a call to "pull.php" that's the long polling of Facebook, i want to reproduce something similar – Jerome Ansia Apr 05 '12 at 15:17
  • I haven't delved into facebook's design much, but I imagine they're using an AJAX call. Pull.php more than likely checks for new information once and updates the page if there is something new, else the AJAX function will wait another x milliseconds and call pull.php to check again. – Blake Apr 05 '12 at 15:19
  • Blake - I've done this before, but mostly because of test harnesses and stuff where I don't want to waste time building an AJAX implementation and want to watch the data changes from some other process. –  Apr 05 '12 at 15:20
  • @Blake you're wrong sorry they don't do it like that if nothing is new the pull.php will timeout ;) – Jerome Ansia Apr 05 '12 at 15:22
  • 1
    I don't know what user load you expect, but long pooling is not a great solution in most of the cases, because you keep a connection open between you client and server, thus removing one slot per user on you http server. (for example, your max connection variable on your server is 100, if you have 100 user using the long polling script, no other new user will be able to connect). Regards – grifos Apr 05 '12 at 15:24
  • thanks grifos i didn't thought of this as well... Currently i'm doing a script that checking every 5 sec for new stuff but that's stupid because 99% of the time you've nothing new so i wanted to improve it – Jerome Ansia Apr 05 '12 at 15:26
  • @Jerome I know that facebook doesn't do long polling like you described for the exact reason grifos just explained. Also, if you're going to be so vague as to what you're doing, how can you expect specific advice? – Blake Apr 05 '12 at 15:26
  • I'm 100% sure that facebook is doing it like that just look at the pull.php when it's timing out or if not the delay between getting new elements. And my question is simple which delay is best between to checks on the database or if it's possible without any delay... – Jerome Ansia Apr 05 '12 at 15:29
  • The answer is simple, does time matter? Is there a huge advantage, disadvantage or does it even matter, if the user receives new information within 1 second, 10 seconds, or 2 minutes? Balance it on its importance. There is no magic number. – Blake Apr 05 '12 at 15:33
  • Yes time matter (that's the point) and 0 second is best... 5 seconds is way too long already 1 second could be fine ;) – Jerome Ansia Apr 05 '12 at 15:34

1 Answers1

1

Yeah, if the while loop runs as fast as it can (it will) you will not just get a fast rate which might overwhelm the database connection, but you'll also not get a very 'even' rate of polling.

Try something like

$polllength = 1;
while(1) {
    $polltime = microtime(true);
    //poll function call;
    $endtime = microtime(true);
    $sleeptime = $polllength - ($endtime-$polltime);
    sleep($sleeptime);
}

This way your polls are about $polllength seconds apart no matter how the polling function varies (it will due to INTARNETS)

EDIT: also, make sure there's a way out of that while loop but everyone should know that y'all

For load balancing, you want to be able to tweak the polllength value somehow, so as to not be completely hardcoded. Whether it be in a configuration file or whatnot is up to you, it might even be a value that increases as load increases. That's up to how it actually 'feels' to the end user and how the server is really faring. A good rule of thumb is that n threads max means n * (poll length / avg response time) users. Reaching beyond that limit would doubtlessly increase response times as the users wait for responses from the overwhelmed server.

  • I'm currently developing an android app with canvas, and it's funny because i use the same to have a constant frame rate. But the question is more which delays is best to not overwhelm the server like half a second or one second, the difference is quiet big if you get a lot of user so... ;) – Jerome Ansia Apr 05 '12 at 15:20
  • That depends on the server more than the code, methinks. But think about it this way. Figure out how long it takes the poll to finish. If it's 100ms, that means with one thread you could potentially have 10 polls a second. If you can have up to 10 threads running, that's 100 users using the service at polltime 1 second resulting in max capacity. –  Apr 05 '12 at 15:22
  • yes and the nb of users, i saw one script one time doing it without any delay... So i was wondering if it was possible to do it like this without breaking down the server – Jerome Ansia Apr 05 '12 at 15:23
  • I think what will happen is that the server will just 'wait' until its ready when it gets overwhelmed. This results in increased delays as users pile up, but setting the poll time to something reasonable based on your capacity might result in a more uniform and thus predictable experience. –  Apr 05 '12 at 15:24
  • Yes i guess there is no way to really know the best delay until experiencing it then, but thanks for your advice and i like the idea of the constant time between 2 checks like you presented it ;) – Jerome Ansia Apr 05 '12 at 15:32