Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Does ajax call keeps an open connection to the server

Posted on 2016-09-21
2
Medium Priority
?
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 59

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

618 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