Push technology in javascript

G

giangiammy

hi all,

I need to implement a web page with a table:
the page structure i fixed, the tables field are updating from time to
time.

The table could be quite big, but just some fields change from time to
time.

I need an update delay of about 1s.

I think that the push technology could fit this requirements: do you
have some
links to examples or documentation of this type implementatiom
in HTML + JavaScript (or eventually using CGI?)

thanks for every information
giammy
 
T

The Magpie

I need to implement a web page with a table: [snip]

I think that the push technology could fit this requirements: do you
have some links to examples or documentation of this type implementatiom
in HTML + JavaScript (or eventually using CGI?)
Javascript (though it can be used elsewhere) is essentially a
web-technology used in session-based interactive operations. What that
means is that it is basically a PULL system, rather than a PUSH one. To
implement a push-based system, you would need the web page to call a
server-side javascript that would then push the data to the web page and
keep itself alive throughought the whole session.

That would, naturally, slow the server down, keep the session busy more
than need be and consume a fair bit of bandwidth. You would also need to
keep checking that the session had not been shut down from the client
side, which is what remains essentially "in-charge" of the session.
 
R

Ronaldo Junior

Javascript (though it can be used elsewhere) is essentially a
web-technology used in session-based interactive operations. What that
means is that it is basically a PULL system, rather than a PUSH one. To
implement a push-based system, you would need the web page to call a
server-side javascript that would then push the data to the web page and
keep itself alive throughought the whole session.

I was thinking about an Ajax request, constantly receiving data
(triggered by readystatechange) and updating the table. If the
connection is interrupted, the readyState would be 4 (completed), so
giving the possibility to catch a possible connection error.

I haven't tested 'cause I don't know how to make a PUSH page, but
anyway, wouldn't it work?
 
T

Thomas 'PointedEars' Lahn

Please provide attribution of quoted material.
I was thinking about an Ajax request, constantly receiving data
(triggered by readystatechange) and updating the table. If the
connection is interrupted, the readyState would be 4 (completed),
so giving the possibility to catch a possible connection error.

Definitely not.

Zeroth, it is not an "Ajax request". Ajax is a buzzword (often used
wrong), not a protocol. What you probably mean is a HTTP request via
an object implementing the IXMLHTTPRequest interface (here: x).

First, you have to understand that HTTP is not a stateful protocol (such as
e.g. FTP is). The server is allowed to end the connection immediately
after it sent the response to a client's request (using Keep-Alive can
prevent that if supported and sent by the client in intervals short
enough.)

Second, x.readyState == 4 merely indicates that the current request-response
cycle has ended. It does not mean that the connection has been severed.

Third, you can detect if a new connection could be established with that
approach or a previous one could be reused. However, in order to determine
if there was an error, you have to look for x.readyState < 4 or
x.status.test(/^(0|5\d\d)/).
I haven't tested 'cause I don't know how to make a PUSH page,

What do you mean by "PUSH page"?
but anyway, wouldn't it work?

Maybe.


PointedEars
 
R

Ronaldo Junior

Thomas said:
What do you mean by "PUSH page"?

Some page providing unlimited, streaming content, like a chat page. I
don't know how to make a page behave like this to test my suggestion.
 
T

Thomas 'PointedEars' Lahn

Ronaldo said:
Some page providing unlimited, streaming content, like a chat page.

You cannot do this exactly with HTTP alone, it required a stateful protocol.
Even a "chat page" (by which you probably mean an HTML document including a
Java applet for the chat; Java != JavaScript) is not such a real-time
application.
I don't know how to make a page behave like this to test my suggestion.

However, you can make requests repeatedly, or even better, make requests
in defined not-too-short intervals. Try this for the second approach:

function update()
{
var me = arguments.callee;

// setRequestHeader(...)

me.x.onreadystatechange = function()
{
if (me.x.readyState == 4)
{
var st = me.x.status;

// request was successful
if (st == 200)
{
// update content in one second from now
me.intv = window.setTimeout("update()", 1000);
}

// request failed
else if (st.toString().test(/^[05])/))
{
// clear interval
window.clearInterval(me.intv);

// (update content to) display error message
}
}
}

me.x.send();
}
update.x = new XMLHttpRequest();
update.x.open("GET", "http://example.com/");

// update content in one second from now
update.intv = window.setTimeout("update()", 1000);

Note that since host objects are involved, you have to do feature tests on
runtime before:

<URL:http://www.jibbering.com/faq/faq_notes/not_browser_detect.html>


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
[...]
// request failed
else if (st.toString().test(/^[05])/))

Should be

else if (/^[05]/.test(st.toString()))

(as it is RegExp.prototype.test().) Note that in JavaScript 1.6
(Mozilla/5.0 rv:1.7, incl. Firefox 1.5), the explicit type conversion
is not necessary:

else if (/^[05]/.test(st))


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top