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

x
?
Solved

Continual Ajax polling every 3 seconds for ever

Posted on 2013-05-31
10
Medium Priority
?
464 Views
Last Modified: 2013-06-06
I am writing a simple Ajax script to check a MySQL database very 5 seconds for any new records and to add them to a table using jquery.

The page may be open for several hours without reload. The server it will be polling is internal, small, and network has very little traffic so the poll speeds will be instant.

Is their anything I need to be careful of or anything I need to do to keep the 5seconds going?

The system runs a messaging system that isn't operated, so it needs to work and cannot rely on people manually refreshing.

Any guidance would be great!

Steve
0
Comment
Question by:Steve Tinsley
  • 5
  • 4
10 Comments
 
LVL 4

Expert Comment

by:Ersoy Hasan
ID: 39210258
One of the important things is to be sure the last query has finished or failed before sending the next one. You could use other techniques for this like :
WebSocket
AJAX long polling
AJAX multipart streaming
Forever Iframe
JSONP Polling

there is a library you can use that takes care for choosing the technique http://socket.io/
0
 

Author Comment

by:Steve Tinsley
ID: 39210270
I've looked into that before but I thought you had to run a difference kind of webserver. We use iis.

Is there a simple example of code you could demonstrate? It's scairs me a bit!

Steve
0
 
LVL 4

Expert Comment

by:Ersoy Hasan
ID: 39210293
With .Net you can use SignalR (https://github.com/SignalR). There is a comprehensive documentation, and simple samples.
0
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.

 

Author Comment

by:Steve Tinsley
ID: 39210304
Oh... I program in PHP. Do I need to look down a different route or will it work with PHP?
0
 
LVL 4

Expert Comment

by:Ersoy Hasan
ID: 39210312
well i can't say anything about php, but you can use the first method you said making a request every few seconds:
Be sure that you exclude the time for the connection to complete
Notify the user that you are refreshing the data
Handle errors
Use setTimeout and on every ajax success/error set another one
0
 
LVL 8

Expert Comment

by:soupBoy
ID: 39210621
What about using jquery's .promise() method?

Then with a little shift in thinking, instead of having your script run every 5 seconds, you could have it run after every 'completed promise que' (don't know what else to call it) but with a 5 second delay....

Hopefully that made sense!
0
 

Author Comment

by:Steve Tinsley
ID: 39212006
Thanks for the help guys. I wonder if you would mind looking at my basic code and recommending any improvements:

function getResults(lastUpdated,questionGroupID) {
	
	$.get('library/process.php?action=checkNewQuestionResponses&questionGroupID=' + questionGroupID + '&lastUpdated=' + lastUpdated, function(data) {	// fetch the json data from the php script.

	var resultSet = jQuery.parseJSON(data);	// parse the json.
	var newlastUpdated = lastUpdated; // update last update variable

		for(var ele in resultSet) {   // loop over each row.
	
			if (resultSet[ele].questionResponseDelete == "1" || resultSet[ele].questionResponseToModerator == "0") {
				removeRow(resultSet[ele]);
			}
			
			else if (resultSet[ele].questionResponseName !== "0") {
				addRow(resultSet[ele]);  // add it in to html.
				if(newlastUpdated == lastUpdated) {
				} 	
			}
			
			var timestamp = (resultSet[ele].timestamp.replace(/[":"]/g, ""));
			var timestamp = timestamp.replace(/[-]/g, "");
			var timestamp = timestamp.replace(/[" "]/g, "");

			if( timestamp > newlastUpdated ) {
				newlastUpdated = timestamp;
			}
		}
	setTimeout("getResults(" + newlastUpdated + ","+questionGroupID+");", 5000);
	});         
}

Open in new window

0
 
LVL 4

Assisted Solution

by:Ersoy Hasan
Ersoy Hasan earned 2000 total points
ID: 39215423
function getResults(lastUpdated,questionGroupID) {
	
	$.get('library/process.php?action=checkNewQuestionResponses&questionGroupID=' + questionGroupID + '&lastUpdated=' + lastUpdated, function(data) {	// fetch the json data from the php script.

	var resultSet = jQuery.parseJSON(data);	// parse the json.
	var newlastUpdated = lastUpdated; // update last update variable

		for(var ele in resultSet) {   // loop over each row.
	
			if (resultSet[ele].questionResponseDelete == "1" || resultSet[ele].questionResponseToModerator == "0") {
				removeRow(resultSet[ele]);
			}
			
			else if (resultSet[ele].questionResponseName !== "0") {
				addRow(resultSet[ele]);  // add it in to html.
				if(newlastUpdated == lastUpdated) {
				} 	
			}
			
			var timestamp = (resultSet[ele].timestamp.replace(/[":"]/g, ""));
			var timestamp = timestamp.replace(/[-]/g, "");
			var timestamp = timestamp.replace(/[" "]/g, "");

			if( timestamp > newlastUpdated ) {
				newlastUpdated = timestamp;
			}
		}
	setTimeout(function () {
                      getResults(newlastUpdated, questionGroupID);
         }, 5000);
	});         
}

Open in new window

You can use $.getJSON instead of $.get, which will return you parsed object.
0
 

Author Comment

by:Steve Tinsley
ID: 39215573
Nice one!! Any other improvements??
0
 
LVL 4

Accepted Solution

by:
Ersoy Hasan earned 2000 total points
ID: 39215792
function getResults(lastUpdated,questionGroupID) {
	
	$.get('library/process.php?action=checkNewQuestionResponses&questionGroupID=' + questionGroupID + '&lastUpdated=' + lastUpdated, function(data) {	// fetch the json data from the php script.

	var resultSet = jQuery.parseJSON(data);	// parse the json.
	var newlastUpdated = lastUpdated; // update last update variable

		for(var ele in resultSet) {   // loop over each row.
	
			if (resultSet[ele].questionResponseDelete == "1" || resultSet[ele].questionResponseToModerator == "0") {
				removeRow(resultSet[ele]);
			}
			
			else if (resultSet[ele].questionResponseName !== "0") {
				addRow(resultSet[ele]);  // add it in to html.
				if(newlastUpdated == lastUpdated) {
				} 	
			}
			
			var timestamp = (resultSet[ele].timestamp.replace(/[":"]/g, ""));
			var timestamp = timestamp.replace(/[-]/g, "");
			var timestamp = timestamp.replace(/[" "]/g, "");

			if( timestamp > newlastUpdated ) {
				newlastUpdated = timestamp;
			}
		}
	        setTimeout(function () {
                      getResults(newlastUpdated, questionGroupID);
                }, 5000);
	}).fail(function () {
             // handle on error 
             setTimeout(function () {
                      getResults(newlastUpdated, questionGroupID);
             }, 5000);
        });         
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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 …
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)
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…
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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