Platinum Partner
dotnet,how-to,tools,web,jquery,net,polling

Quick Tip – Long Polling Using jQuery

Yesterday, I helped to write infrastructure code for a JavaScript SDK API that one of my customers is building right now. One of the things that needed to be implemented as part of the SDK was the functionality of long polling. Since the SDK has a dependency on jQuery, here is the code snippet that we used as starting point to write the implementation:

function longPoll(){
    $.ajax({ 
        url: "put here the server endpoint url",
        success: function(data){
            // do whatever you want with the data you received
        },
        error: function(err) {
            // do whatever you want when error occurs
        },
        type: "GET", 
        dataType: "json", 
        complete: longPoll,
        timeout: 60000 // timeout every one minute
    });
}

In the snippet, you use the $.ajax function to start the long polling and in the complete option you use the same caller function (longPoll) to continue polling the server. Another thing to notice is the timeout option which is set to one minute.

The code is really simple and I hope it will help you the get started with long polling.

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}