/    Sign up×
Community /Pin to ProfileBookmark

HTTP "Service Streaming" and Javascript

According to a page about “HTTP Streaming”:

“The responseText property of XMLHttpRequest always contains the content that’s been flushed out of the server, even when the connection’s still open.”

So I want to open an HTTP connection to a page that continually spits out data, and every few seconds I want to read the last line that was spit out. But it apparently “only works on Firefox, whether XMLHTTPRequest or IFrame is used. In both cases, IE suppresses the response until its complete.”

Is that accurate? Is there any way in javascript to read the server’s response before the server finishes responding?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMay 08.2008 — A better question is, why are you trying to stream text? Because of the cross-browser issues, and that text isn't a large amount of data, you can fake the streaming effect by progressively showing the responseText using a setTimeout or setInterval after the responseText is fully downloaded. Does your application require the text to be streamed, or is it just for eye candy?
Copy linkTweet thisAlerts:
@trevordixonauthorMay 08.2008 — No I'm loading data as it changes. Right now it's polling the server every few seconds, and all the opening and closing of connections is wreaking havoc on the server. I want it to open one connection then keep getting the data every few seconds from that connection.

var xhReq = createXMLHttpRequest();

window.onload = function() {

setInterval(periodicXHReqCheck, 100);

xhReq.open("GET", "countdown.phtml?start=3", true);

xhReq.onreadystatechange = function() {

if (xhReq.readyState==4) { /* alert("done!"); */ }

}

xhReq.send(null);

}

function periodicXHReqCheck() {

var fullResponse = util.trim(xhReq.responseText);

var responsePatt = /^(.*@END@)*(.*)@END@.*$/;

if (fullResponse.match(responsePatt)) { // At least one full response so far

var mostRecentDigit = fullResponse.replace(responsePatt, "$2");

$("response").innerHTML = mostRecentDigit;

}

}

This method, used at http://www.ajaxify.com/run/streaming/xmlHttpRequest/countdown/ works in Firefox, but Internet Explorer and Safari don't allow access to xhReq.responseText until the server's done sending the data. Is there any way to make this work in other browsers? I'll use IFrames if need be…
×

Success!

Help @trevordixon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...