Solved

Does ajax call keeps an open connection to the server

Posted on 2016-09-21
2
50 Views
Last Modified: 2016-09-27
Hi,
I came across the following code on this site :
https://gist.github.com/jhbsk/4353139
// Long Polling (Recommened Technique - Creates An Open Connection To Server ∴ Fast)
(function poll(){
    $.ajax({ url: "server", success: function(data){
        //Update your dashboard gauge
        salesGauge.setValue(data.value);
    }, dataType: "json", complete: poll, timeout: 30000 });
})();

Open in new window


Although i dont see in documentation of ajax call that this will keep an open connection to the server.
And in one of the places where i did see this type of code being used the following was the content in network tab in chrome :
Screen-Shot-2016-09-21-at-5.50.42-PM.pngWhich means that indeed there is no time spent on Initial connection setup, SSL etc.
Please suggest what you think of this approach.

Thanks
0
Comment
Question by:Rohit Bajaj
2 Comments
 
LVL 9

Assisted Solution

by:Shahid Thaika
Shahid Thaika earned 150 total points
ID: 41808672
Normally AJAX does not keep a connection open. The code is a jQuery way of making an AJAX call. Once the call is made to the server and a response is received, then the code in the Success parameter is executed. If there is a failure, then the function in the Error parameter is executed, but that is not mentioned here.

Once both of those are done, the code / function against Complete is executed. The Complete parameter above is calling the Poll function, which in turn will make the AJAX call, and this will keep triggering the AJAX call to the server infinitely.

In terms of suggestions, personally I'd write a function for Complete which will call the Poll function using a timer (setTimeOut), so that you make fewer calls to the server. You also need an additional lines of code in Success to see if you are running the code ifor too long.
0
 
LVL 51

Accepted Solution

by:
Julian Hansen earned 350 total points
ID: 41808716
That is basically calling itself - on complete it calls poll and repeats.

This is a quick and dirty way of doing this. A better solution (one used by solutions like Firebase) - is to use web sockets and have changes pushed to the client by the server.
These articles may be of interest if you want to know more about web sockets
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications
https://developer.mozilla.org/en/docs/Web/API/WebSocket
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API
1

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now