I'm implementing Comet with long-polling AJAX and Apache/PHP. The problem is that if there is not interaction in a (unknown for me) period of time, ie, no data sent/received, and then after some time, I send an event, the client-side long-polling request keeps pending without receiving the new data. Sometimes client-side receives a timeout and reconnects, but sometimes it doesn't receives anything and, as I already said, the request keeps pending. I'm implementing it as indicated here with some alterations unrelated to the comet implementation per se (ie: using database instead of file). It uses a loop on the server-side waiting for new events and seems that server timeout closes the connection but client doesn't receives the connection close for reconnecting. How can I avoid this problem? Would the solution be using a client-side timer for reconnecting?
1 Answer
You need to never wait more than 50 seconds to send a response, otherwise the browser may time out. Basically if there is nothing new to report after 50 seconds, send a response anyways. This empty response will trigger the client to send a new request to the server and start over again.
4 Comments
Alejandro García Iglesias
The problem is that when browser times out, it closes the request. That way my comet object in JavaScript sends another request for events to the server. But this case it seems that there's something on server side closing the request and the client still waiting for response.
dqhendricks
@GarciaWebDev its well known that shouldn't keep a request open for over 50 seconds. Try it, and you will see it works.
Alejandro García Iglesias
I put a timeout of 30 secs and worked fine. Any particular reason for choosing the number 50 secs? Is it a convention? How much safe is 50 secs?
dqhendricks
@GarciaWebDev I have read in articles written by engineers at large social media companies saying that their research shows 50 seconds is the longest time you can use while making sure it is safe for a wide range of clients. You will want to push it this high to reduce the number of required requests on your system.