[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Does ajax call keeps an open connection to the server

Posted on 2016-09-21
2
Medium Priority
?
178 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 600 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 60

Accepted Solution

by:
Julian Hansen earned 1400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
Suggested Courses

873 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