Comet connection keeps closing after every minute.

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Comet connection keeps closing after every minute.

Post by JellyFish »

I am using a long polling comet technique in my application that polls for information from a page which loops forever until it finds new data to send back. Everything works, except it seems that every minute Firefox closes my polling connection; as if there was a limit to how long Firefox waits for a page. It could be something other then Firefox because there's this other comet example/tutorial that doesn't appear to suffer from this issue.

I'm thinking it could either jQuery (which I'm using to make the XHR requests) or it could be my server. If it's my server, which I'm betting it is, then how could I turn off this restriction, if possible? I'm on a shared hosted server, so I believe it to be very plausible that my hosting provider would create this restriction for their costumers.

Has anyone ever had anything similar to this happen? What could be the cause?

Thank you for reading.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Comet connection keeps closing after every minute.

Post by kaszu »

I think it's server. PHP by default has 30 ('max_execution_time' configuration name) seconds, before script is terminated. It can be changed by ini_set, but it will terminate (by apache?) after some time anyway, last time I tried, I think it was about 10 or 20 minutes.
I usually set few minute period and if after this period there is still no data to send, then close the connection on server. On client side when connection is closed, run another request.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Comet connection keeps closing after every minute.

Post by JellyFish »

Thanks man. So I guess:

Code: Select all

ini_set("max_execution_time", 20*60);
ought to do it.

[EDIT] Actually, it seems as thought it's still closing even with this php statement at the top of my php page. But it's closing at 2 minutes for the first request, and 1 minute on the following requests. Strange.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Comet connection keeps closing after every minute.

Post by Chris Corbyn »

I think you need to find a way to push "inert" data down the connection, such as a header, or an X-Mixed-Replaced entity every 10 seconds or so. Both the browser and the server have timeouts and if the server is not responding the browser will give up.
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Comet connection keeps closing after every minute.

Post by JellyFish »

Chris Corbyn wrote:I think you need to find a way to push "inert" data down the connection, such as a header, or an X-Mixed-Replaced entity every 10 seconds or so. Both the browser and the server have timeouts and if the server is not responding the browser will give up.
I don't think I understand the first sentence. What's inert data, and what's an X-Mixed-Replace entity?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Comet connection keeps closing after every minute.

Post by VladSun »

Code: Select all

set_time_limit(0);
//ignore_user_abort(true);
 
$waitTime = 1;
 
$separator = "string_not_presenting_in_data";
$boundary = "\n" . $separator . "\n";
 
header('Content-Type: multipart/x-mixed-replace; boundary=' . $separator);
header("Pragma: no-store, no-cache");
header("Cache-control: no-cache, no-store, must-revalidate, max-age=-1");
header("Expires: -1");
 
echo $boundary;
 
for ($i=0; $i<5; $i++)
{
    echo "Content-Type: text/plain\n\n";
    echo "#".$i."\n";
    echo $boundary;
    ob_flush();
    flush();
    sleep($waitTime);
}
it doesn't work in Opera, though.
It doesn't work in Chrome either, but it works in Safari, FF and IE.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply