Jump to content

Ajax OR httpstream


drewbee

Recommended Posts

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 streaming
or
polling?

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??
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 100k

What is the proper way to destroy an object from memory?
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.