Reputation: 1974
I'm looking in to doing long polling for a "notifications" system on a website. I've never done this before and thus I'm looking for information online about it. I am looking here: http://techoctave.com/c7/posts/60-simple-long-polling-example-with-javascript-and-jquery
In the section about long polling it says: "Timeout: Set a timeout (in milliseconds) for the request. The timeout period starts at the point the $.ajax call is made. Here, we set the timeout to 30 seconds. This means our poll function won't get called again until both the ajax call is complete and (at-least) thirty (30) seconds have passed."
Saying poll won't be called until BOTH the ajax call is complete AND at least 30 seconds have passed.
When I add this in to Javascript though, poll is called over and over again in just the time it takes for the ajax call to finish. It doesn't wait 30 seconds.
(function poll() {
$.ajax({ url: "/myapp/messages/messages/checkMessages", success:
function(data){alert("YO");}, complete: poll, timeout: 30000 });
})();
Am I doing something wrong, or is this how long polling is suppose to work?
Also, is this efficient? Would this scale with site traffic?
Thanks for the help!
(function poll(){
setTimeout(function(){
$.ajax({ url: "/myapp/messages/messages/checkMessages", success: function(data){
console.log("Polling!");
}, complete: poll, timeout: 30000});
}, 30000);
})();
Upvotes: 3
Views: 5469
Reputation: 177
Seems like you experienced the session file lock
For PHP
Use session_write_close()
Upvotes: 1
Reputation: 21
The reason for doing a long poll is to get the effect of a server push. This means that there is an unpredictable delay on the server side during which, ideally, the client side is simply waiting asynchronously for the server to send a message. The server cannot initiate a connection with the client, so the client initiates the connection and then waits for the server to have something to say.
If that's what you have in mind, then there is no reason to impose timeouts on the AJAX call; well, there's one: if you're concerned that the connection to the server might drop, or that the server itself might hang indefinitely, then putting a timeout on the AJAX call will cause the client to re-initiate the connection after the given time, regardless of whether the server has transmitted anything. Absent any disconnection with the server, having a timeout on the client side makes no difference at all.
The original question states that the server is responding instantly to every ajax request, and it sounds like the reason for wanting a timer (not a timeout) on the client side was to slow down the conversation with the server. If that's the case, this is really not long polling, it's just plain vanilla polling. The difference in a nutshell is: with polling, the client controls the timing and frequency of information flowing from the server. With long polling, the server controls the timing and frequency. The whole idea is to eliminate any delay between the server sending data and client acting on that data. Using a timer on the polling side to delay responses from the server defeats that purpose.
You can do effective long polling without any timeout on the client side. As in the examples already offered, use the 'complete:' definition to restart the ajax request; use the 'success' definition to process any data. 'complete' will happen whether the XHR succeeded or not.
If you want to make it immune to dropped connections, then you can achieve a dead-man switch by doing a timeout on the server side. On the server, have it reply with a heartbeat even if it has no data to send. In the success: definition, update a local time-of-last-heartbeat. In the complete: definition, panic or fail or do what you want to do when there has not been a heartbeat in longer than the server timeout is set for.
Upvotes: 2
Reputation: 51780
You don't see the need for the complete
callback if it is meant to be called every 30 seconds anyway :
(function poll(delay){
$.ajax({
url: '/myapp/checkMessages',
timeout: delay,
success: function(data){
console.log('Messages !');
}
});
setTimeout(function(){ poll(delay); }, delay);
})(30000);
Upvotes: 0
Reputation: 503
Setting the timeout on the $.ajax
call will only set the maximum timeout for the call.
i.e. if the first call to $.ajax
has not completed within the timeout time then poll will be called again. Otherwise if the call is complete then poll
will be called again. This will cause poll
to be called repeatedly if it completes before the timeout time.
Below wraps the poll
in a setTimeout stopping poll
from repeatedly calling itself (as you have done above)
(function poll(){
setTimeout(function(){
$.ajax({ url: "/myapp/messages/messages/checkMessages", success: function(data){
console.log("Polling!");
}, complete: poll, timeout: 30000});
}, 30000);
})();
*note: I have added an answer for anybody looking for more clarity.
Upvotes: 5