Solved

Continual Ajax polling every 3 seconds for ever

Posted on 2013-05-31
10
459 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
[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
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 500 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

690 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