Quick Tip – Long Polling Using jQuery

August 26, 2013

one comment

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.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

one comment

  1. AdedeAstespamDecember 16, 2013 ב 23:15

    Hello Mr Fink.

    I am a noob at this and I am wondering by what I am suppose to write in the URL:? I have a host and a test site but it does not seem to work out for me. Example such: URL: “http://mySite/Test/test.txt”, but my text is not updating? Could you help me out here?
    Thanks.

    Reply