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

[eluser]slowgary[/eluser]
I'm not sure what a kettle of fish is, but I find this very fun Wink

I have seen that article, and one other. They were they only examples I'd seen of the server side of things, and partially what I'd based mine on, but it still just didn't sit right for those reasons listed above.

After talking with a friend much smarter than me, I have a few answers. To my questions. For anyone else on this quest, here they are.

1. Performance of sleep()
==============================
The call to sleep() indeed does not use any CPU, which makes this method acceptable performance-wise. It also isn't counted against your script's max execution time.

2. Rogue infinite while loops
==============================
I'm not 100% sure on this one, and some of the info I got still hasn't sunk into my mind, but there are several reasons that were explained to me that make this a non-issue. The obvious possibility is that this script could hit the max execution time threshold. This should probably be taken into consideration when writing this type of script. Getting a timestamp at the top and checking to see if(now() - timestamp > max execution time) would be one way to handle it. The other factor here is at the socket level. I could be wrong but I think when the browser leaves the site it sends notification to the server that it's closing the connection, and the script is killed.

3. Data for multiple users
==============================
In my simple example above, I just deleted the data after getting it and that took care of making sure that you don't send old data over and over to the user. In a real-world example though, you'll likely have multiple users who need the data, so deleting is not an option. You'd need to track it with time. The idea here would be to track the time that new data is added, and the time that a user last checked for data. By comparing these two, you can always know which data is new and which data is old for a particular user. I initially had issues figuring this out because my idea was to save a timestamp at the beginning of the PHP script, and only fetch data that had been added after. The problem is that new data might be added between AJAX requests, and the user would never receive it. The solution is obvious, you'd need to store the last time of update per user, and update that timestamp every time the user gets new data.


I hope this helps someone else.


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