Jump to content

Long polling with PHP


Cupidvogel

Recommended Posts

Hi, I was reading an article on long-polling at http://www.nolithius.com/game-development/comet-long-polling-with-php-and-jquery where I found the following lines (scroll down a little in that page to view this, under the PHP sleeps across the entire session heading):

 

When you make a call to sleep() or usleep(), PHP will pause execution across the entire session, which means that any other AJAX requests or even pageloads will have to wait until the request returns or times out. To address this, ensure the session is closed before entering the serverside sleep loop.

 

I didn't quite understand what it means. Suppose I have a webpage foo.php, and it contains two updateable parts, one calls coo.php to update asynchronously, while the other calls goo.php. Now both coo.php and goo.php themselves poll the database, returns the result set if they get any, or wait for, say, 15 seconds before polling the database again, and this continues, until the total execution time exceeds 100 seconds, upon which PHP results the empty (or populated) result-set. The Javascript on the client side receives that empty or populated data and immediately sends another request. Now aren't these two requests to 2 different scripts (albeit from the same script) independent of each other, or is it that when coo.php sleeps, foo.php and goo.php is forced to sleep as well?

Link to comment
Share on other sites

The issue is if you are using sessions (ie, call session_start()).  PHP Locks the file that stores the session data so only one script/process can access it at a time.  So if you make a request to coo.php and it starts the session then PHP locks the file.  While it is sitting there doing it's polling your request to goo.php will be blocked at the session_start() call waiting for coo.php to unlock the session file so it can use it.

 

There are three ways to avoid this problem.

1) Don't use session in your long poll scripts, not very useful as you often need the session data.

2) Call session_write_close() to end the session and free the file.  Your $_SESSION variable will still exist but you won't be able to write to the session.

3) Implement your own session handler using something like Mysql or Memcached so that there is no longer an issue with locks.

 

#3 would be the ideal solution.  #2 will work in most cases since generally you only need to read the session data for login/user id details and not write anything.

Link to comment
Share on other sites

Please explain solutions 2 and 3 in detail, I am kind of new to PHP! When should I call session_write_close?  What is meant by session data? I mean, once the user is logged in, how can a script log out of it during a polling request while the user still continues to browse other pages and expect the pages to be personalized? How come MySQL and memcache help here?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.