Solved

Does ajax call keeps an open connection to the server

Posted on 2016-09-21
2
74 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 54

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

810 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