Basically my answer was to switch from long polling to short polling. This was wrong of me. I am a bad answerer.
clarification lives here, along with some pros/cons and possibly a reason for your error:
Scaling a chat app - short polling vs. long polling (AJAX, PHP)
First glance: I think the issue you're running into is that you're polling every second, but telling the server to sleep for a second. if you change that
if to a
while then you'll be telling it to sleep indefinitely, until a change has been made to the db and the
$currentmodif is updated. What you want to do is send a response back to the js, not wait for a change to be made before responding.
instead try this logic:
- send the timestamp through as you're doing, but if the database hasn't been updated, just return an empty response or a code you can recognize as "not updated" and do nothing.
- get rid of that "sleep", theres no need to wait to return a response.
setIntervalinstead of a recursive
setTimeout, and keep a reference to that interval around so you can
clearIntervallater and stop the polling.
- I'm 99% positive that facebook and other services use websockets instead of polling (at least for any browsers that support it). you may want to investigate this.