javascript - polling vs long polling -


i got onto these examples showing polling vs long-polling in javascript, not understand how differ 1 another. regarding long polling example, how keep connection open?

this traditional polling scenario looks like:

(function poll(){   settimeout(function(){     $.ajax({ url: "server", success: function(data){       //update dashboard gauge       salesgauge.setvalue(data.value);        //setup next poll recursively       poll();     }, datatype: "json"});   }, 30000); })(); 

and long polling example:

(function poll(){   $.ajax({ url: "server", success: function(data){     //update dashboard gauge     salesgauge.setvalue(data.value);    }, datatype: "json", complete: poll, timeout: 30000 }); })(); 

thanks!

the difference this: long polling allows kind of event-driven notifying, server able actively send data client. normal polling periodical checking data fetch, say. wikipedia quite detailed that:

with long polling, client requests information server in way similar normal polling; however, if server not have information available client, instead of sending empty response, server holds request , waits information become available (or suitable timeout event), after complete response sent client.

long polling reduces amount of data needs sent because server sends data when there data, hence client not need check @ every interval x.

if need more performant (and imho more elegant) way of full duplex client/server communication, consider using websocket protocol, it's great!


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -