Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  126] [ 1]  / answers: 1 / hits: 34614  / 11 Years ago, sun, september 1, 2013, 12:00:00

Hi I understand that in long polling you keep the connection with the server open for long till you a get a response back from the server and then poll again and wait for the next response. However i dont seem to understand how to code it. There is this code below which uses long polling but I dont seem to get it



(function poll(){
$.ajax({ url: server, success: function(data){
//update page based on data

}, dataType: json, complete: poll, timeout: 30000 });
})();


But how is the connection kept open here. I understand that poll function is fired again once the response from the server is got.But how is the connection kept open?



Edit1:- It would be great if someone can also explain what would timeout actually do here


More From » jquery

 Answers
19

The client cannot force the server to keep the connection open. The server is simply not closing the connection. The server will have to say at some point that's it, there's no more content here, bye. In long polling, the server simply never does so and keeps the client waiting for more data, which it trickles out little by little as updates come in. That's long polling.



On the client side it's possible to check occasionally for the data which has already been received, while the request has not finished. That way data can occasionally be sent from the server over the same open connection. In your case this is not being done, the success callback will only fire when the request has finished. It's basically a cheap form of long polling in which the server keeps the client waiting for an event, sends data about this event and then closes the connection. The client takes that as the trigger, processes the data, then reconnects to the server to wait for the next event.


[#75968] Friday, August 30, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jeffery

Total Points: 180
Total Questions: 114
Total Answers: 117

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;