drewbee Posted January 12, 2007 Share Posted January 12, 2007 I am currently working on an application that will require the current user of a page to see updated results within a seconds time (and will update/change approximately every second or two).So, I am wondering what is better ?http streamingorpolling?I have read that http streaming does not scale well. With polling I will be making a call to the server every second gathering data.Polling is easier to setup/maintain as well.What are your guyses thoughts/opinions/comments on these two types of AJAX data retrieval?? Quote Link to comment Share on other sites More sharing options...
ober Posted January 12, 2007 Share Posted January 12, 2007 I'd go for polling, but I've never had much experience with streaming.If you spilled some details on the type of data and how much, it might help us a little better.Keep in mind that if you poll the server every second, one request may not complete before the next one is made. You better get friendly with the abort() function. Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 12, 2007 Author Share Posted January 12, 2007 Yeah :) Abort is a wonderful thing. I call it on any previous activities before creating the new request. It is really not going to be that much data, however, the data will be constantly be retrieved from a database. So in all reality, I am starting to think polling would be a better idea anyways; since the database will need to make its own call as well.For my AJAX library, I use objects. It helps with that silly error that Firefox likes to send when one request is used over the top of another. Perhaps you can guide me when doing this as well.As for the polling part, I recreate the same object then the request.Do you think this is a good setup, or is it better to only create one object and resend the request multiple times?Heres an example of what I do:[CODE] function doLoop() { var ajax = new AjaxObject(); ajax.sendRequest('action=blah&foo=bar'); } setInterval('doLoop()',100);[/CODE]Would this be better?[CODE]var ajax = new AjaxObject(); function doLoop() { ajax.sendRequest('action=blah&foo=bar'); }[/CODE] Quote Link to comment Share on other sites More sharing options...
ober Posted January 12, 2007 Share Posted January 12, 2007 I think you'd be better of using one object. I'd be afraid of a memory leak using the other option because you're constantly creating instances of the object without destroying them. Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 12, 2007 Author Share Posted January 12, 2007 Interesting. The only thing against the first method is that since it is being assigned to a variable, doesn't the object only reside within that variable, and the creation of a new variable will simply over write the other variable? Or is my thinking off base on this one? Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 12, 2007 Author Share Posted January 12, 2007 or even better, destroying the object before the new one is created? Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 12, 2007 Author Share Posted January 12, 2007 Here we go....thoughts?[CODE] function doLoop() { if (ajax) { ajax = null; } var ajax = new AjaxObject(); ajax.sendRequest('action=blah&foo=bar'); }[/CODE]This should now destroy and remove any reference to the old object if it exists. Then it is simply re-created in the same space. Quote Link to comment Share on other sites More sharing options...
ShogunWarrior Posted January 12, 2007 Share Posted January 12, 2007 In Javascript as long as you declare it [b]var ajax[/b] then it will be available in the function. Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 12, 2007 Author Share Posted January 12, 2007 I know that it is globally available.This is not the question at hand :)The question at hand is properly destroying objects as to not create memory leaks when re-creating hundreds of thousands of the same object into the same variable ;D Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 12, 2007 Author Share Posted January 12, 2007 Well, this does not solve the problem of a meory leek. I left a small ajax program run (that just returns the current time every second).I am monitoring windows task manager and looking at iexplore.exe and it is doing nothing but increasing... it is currently gained approximately 40k in the last 10 minutes and currently at 100kWhat is the proper way to destroy an object from memory? Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 13, 2007 Author Share Posted January 13, 2007 Ober,I have placed the object creation outside of the function as said, and you were correct. The memory does not rise one bit as the page runs. I seem to be having some problem getting abort to run properly now though. At what point would you suggest putting this in a object based ajax class? Quote Link to comment Share on other sites More sharing options...
ober Posted January 13, 2007 Share Posted January 13, 2007 Glad it worked. Can you post the code where you use the abort? Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 13, 2007 Author Share Posted January 13, 2007 Here is the code for my object based AJAX.[CODE]function AjaxObject101() { this.createRequestObject = function() { try { var ro = new XMLHttpRequest(); } catch (e) { var ro = new ActiveXObject("Microsoft.XMLHTTP"); } return ro; } this.sendRequest = function(data) { this.abort; var action = 'POST'; var url = '/ajax_requests.php'; if (action.toUpperCase() == "POST") { this.http.open(action,url,true); this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); this.http.onreadystatechange = this.handleResponse; this.http.send(data); } else { this.http.open(action,url + '?' + data,true); this.http.onreadystatechange = this.handleResponse; this.http.send(null); } } this.handleResponse = function() { if ( me.http.readyState == 4) { if (typeof me.funcDone == 'function') { me.funcDone();} var rawdata = me.http.responseText.split("|"); for ( var i = 0; i < rawdata.length; i++ ) { var item = (rawdata[i]).split("=>"); if (item[0] == "ERROR") { alert('An Error Has Occured:\n' + item[1]); } else if (item[0] != "") { if (item[1].substr(0,3) == "%V%" ) { document.getElementById(item[0]).value = item[1].substring(3); } else { document.getElementById(item[0]).innerHTML = item[1]; } } } } if ((me.http.readyState == 1) && (typeof me.funcWait == 'function')) { me.funcWait(); } } var me = this; this.http = this.createRequestObject(); var funcWait = null; var funcDone = null;}[/CODE]It is a modified version of feather ajax. Quote Link to comment Share on other sites More sharing options...
drewbee Posted January 13, 2007 Author Share Posted January 13, 2007 Abort doesn't seem to work (and yes i know I the () is missing from the abort command).I also tried doing abort in the main loop call with limited luck[CODE] var ajax = new AjaxObject(); function doLoop() { ajax.abort(); ajax.sendRequest('action=blah&foo=bar'); }[/CODE]It gives reference to abort not existing, which makes sense as I don't have a method abort() within the object class.Hmm does this mean just make a simple abort method of which contains a this.abort() ??At the same time, I would figure abort would work where I originally have it called from, but I guess not. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.