Solved

Does ajax call keeps an open connection to the server

Posted on 2016-09-21
2
116 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 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 57

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 Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
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…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

688 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