Continual Ajax polling every 3 seconds for ever

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
Steve TinsleyAsked:
Who is Participating?
 
Ersoy HasanConnect With a Mentor Commented:
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
 
Ersoy HasanCommented:
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
 
Steve TinsleyAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Ersoy HasanCommented:
With .Net you can use SignalR (https://github.com/SignalR). There is a comprehensive documentation, and simple samples.
0
 
Steve TinsleyAuthor Commented:
Oh... I program in PHP. Do I need to look down a different route or will it work with PHP?
0
 
Ersoy HasanCommented:
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
 
soupBoyCommented:
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
 
Steve TinsleyAuthor Commented:
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
 
Ersoy HasanConnect With a Mentor Commented:
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
 
Steve TinsleyAuthor Commented:
Nice one!! Any other improvements??
0
All Courses

From novice to tech pro — start learning today.