tags:

views:

93

answers:

2

I asked a previous question about keeping up a constant link with a database, and someone suggested long polling. I found a small tutorial with a skeleton, but I don't understand some things. The page is here: http://www.google.ca/url?q=http%3A//www.ajaxprojects.com/ajax/tutorialdetails.php%3Fitemid%3D575&ei=AsIzS8nYHNDDlAfY1NSjBw&sa=X&oi=spellmeleon%5Fresult&resnum=2&ct=result&ved=0CAoQhgIwAQ&usg=AFQjCNFjVZDDj1TvYOmNccgaV5XI0rnv9g

First, what happens after 30 seconds? Does it quit? Second, why does it sleep if there's no new content.

+1  A: 

I think your interested in Comet Programming, which is a simulation of a two way socket using http (ajax). This link should help answer your questions. http://en.wikipedia.org/wiki/Comet%5F%28programming)

Kendall Hopkins
A: 

In that example, the loop in the server script will iterate no more than twice. So the request won't be open too long.

You could just as well do the sleep on the client side.

It makes sense to poll at an interval so that you don't put too much load on the underlying resource being queried.

jspcal
But what is the sleep for?
Hussain
The sleep reduces the load on the data source by spacing out the queries. Querying the data source constantly with no delay would use up a lot of resources. The sleep interval should be something appropriate for your app. Doesn't have to be 30 seconds, it can be much lower.
jspcal
But one line says 'sleep(25000'); What is that for? Is that needed?
Hussain
The sleep is needed in this case, but the duration is up to you. It can be done on the server or the client. That prevents a hard loop that constantly queries the data source.
jspcal