Jump to content

Polling alternative


Chris92

Recommended Posts

Is there an alternative to polling a MySQL database?

 

Basically I have a PHP script that polls a table every half a second. This script then returns a value to a client that is doing a reverse-ajax request on the server.

 

Is there a different technique that can be used, so MySQL doesn't return until there is an update rather than polling for an update.

 

Thanks in advance,

Chris

Link to comment
Share on other sites

Wasn't really what I was looking for. It just explains reverse-ajax. What I'm looking is more of a MySQL to PHP push technique. Not PHP to JavaScript.

 

What my PHP script does is this:

 

$query = mysql_query("SELECT * FROM `table` WHERE `time` > ". (int)$_SESSION['last_package_time'] ."");
$rows = mysql_num_rows($query);

while( $rows == 0 )
{
    $query = mysql_query("SELECT * FROM `table` WHERE `time` > ". (int)$_SESSION['last_package_time'] ."");
    $rows = mysql_num_rows($query);
    set_time_limit(1);
    usleep(250000);
}

echo json_encode(mysql_fetch_assoc($query));

 

Which as you can see is rather straining on the MySQL server. There's bound to be a way for MySQL to not return a value until the query has a value.

Link to comment
Share on other sites

Which as you can see is rather straining on the MySQL server. There's bound to be a way for MySQL to not return a value until the query has a value.

 

Even if there was (which there isn't) you would still need to query the database to find out (which is actually what you are doing).

Link to comment
Share on other sites

It's for a web game. The thing is the game has to be accessible by an API, for cross platform compatibility, so I'm storing everything in a database rather than a cache system.

 

The web application works like any other typical web system would. It sends data through one Ajax request and gets it through another.

 

The getting part uses reverse-ajax. So the client doesn't poll the server when there's no need for it. Then the server runs a PHP script that polls the MySQL server for updates which then returns the new values to the HTTP server which then returns data to the client.

 

I have read up on PostgreSQL and it has listen and notify functionality. Does MySQL not come with the same?

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.