Search code examples
phpjavascriptajaxapachecomet

Comet implementation with AJAX and PHP (on Apache) drops connections


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?


Solution

  • 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.