1

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 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
@GarciaWebDev its well known that shouldn't keep a request open for over 50 seconds. Try it, and you will see it works.
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?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.