Search code examples
settimeoutlong-pollingpolling

Difference between long polling and setTimeout


What is the differnce between Long-Polling and setTimeout I found this for long polling but it could not make any difference from setTimeout.

(function poll(){
    $.ajax({ url: "server", success: function(data){
        //Update your dashboard gauge
        salesGauge.setValue(data.value);

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

Solution

  • I guess what your code does is normal polling.

    Long polling keeps the client request open until new data is there to be send to the client. Wikipedia describes it at best:

    With long polling, the client requests information from the server exactly as in normal polling, except it issues its HTTP/S requests (polls) at a much slower frequency. If the server does not have any information available for the client when the poll is received, instead of sending an empty response, the server holds the request open and waits for response information to become available. Once it does, the server immediately sends an HTTP/S response to the client, completing the open HTTP/S Request. In this way the usual response latency (the time between when the information first becomes available and the next client request) otherwise associated with polling clients is eliminated.