Welcome Guest, Not a member yet? Register   Sign In
Long-Polling AJAX requests
#1

[eluser]slowgary[/eluser]
Hey party people.

I'm tinkering with writing a server-push style application. Since most server-push technologies aren't widely supported, I've settled on long-polling AJAX requests. There's just not a lot of resources out there on the server-side implementation. That's hopefully where you come in.

This is what I have so far:
Code:
<?php

$count = 0;

while($count++ < 12)
{
    if($result = mysql_fetch_assoc(mysql_query("SELECT * FROM testing LIMIT 1")))
    {
        mysql_query("DELETE FROM testing WHERE id = ".$result['id']);
        die($result['text']);
    }
    else
    {
        sleep(5);
    }
}
echo 0;
?&gt;
It's a turd so far, but I just wanted to get the basic gist of it. I just have so many questions. I read that sleep() time doesn't get counted against PHP's max execution time. Does it free up the server to process other requests? It must use SOME cpu cycles to at least keep track of the time. Is there a more efficient way to do this?

Also, I figure if the user refreshes the page, this script would still run until there's new data. If a bunch of these 'rogue' scripts were running and no new data came in, they'd waste lots of cycles. That's why I threw the $count in (originally is was while(true), yuck.). It's just a safeguard so that a single instance of those script will never live for more than a minute or so. Does this seem like a sound idea? Is there a better way to do that?

Lastly, this is a simplified example, but in real world usage it would likely need to track the time of an event, instead of just deleting old events. Otherwise the app would not work for more than 1 person. The problem is, how can you adequately track the time? If I create a timestamp at the top of the script and search for rows that happen after the timestamp, there's still the chance that the client could miss new data that gets inserted between AJAX calls. What's the best way to overcome this?

I hope this all makes sense. It's new to me, but very interesting. Much more fun than the average business app (CRUD, CRUD and more CRUD).


Messages In This Thread
Long-Polling AJAX requests - by El Forum - 09-10-2009, 11:31 PM
Long-Polling AJAX requests - by El Forum - 09-10-2009, 11:57 PM
Long-Polling AJAX requests - by El Forum - 09-11-2009, 07:36 PM
Long-Polling AJAX requests - by El Forum - 09-11-2009, 07:53 PM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 12:55 AM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 01:11 AM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 04:29 PM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 04:30 PM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 05:56 PM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 07:55 PM
Long-Polling AJAX requests - by El Forum - 09-16-2009, 09:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB