Re: Extension methods & XMLHttpRequest

Stefan Eissing wrote:
> I am sure that David can speak for himself, however the benefits of  
> IFrames over XmlHttpRequest are discussed in the "Comet" web pattern.  
> For example here:
> 
> http://softwareas.com/index.php?s=comet

Funnily enough I was working through the details last night of
streaming messages to a client asynchronously.  (For example, like in
an instant message client, live news feed, shared whiteboard, etc.)

I came to the same conclusion as Comet: you have to use an IFRAME and
stream SCRIPT elements to it as each message is to be transferred.

Technologically it doesn't look pretty, but the mess is really
confined to one small thing: the code responsible for streaming
messages.  The rest of the application code is decoupled from this
mechanism.

It's interesting that this method is quite portable, works with very
old browsers, and is close to optimal in terms of network performance
and responsiveness.  (Compare with polling using XHR every few
seconds, as some people do).

There's actually nothing to be gained by using XMLHttpRequest on
browsers that support incrementally viewing the response (not IE), and
plenty to lose.

Ways in which you lose with XHR are: you'd still have to poll the XHR
object often; xhr.responseText might get progressively slower if it
always re-parses the received bytes into characters; you can't easily
tell if a browser's XHR is incremental except by sniffing the User-Agent.

(Btw, Gecko and Microsoft's XHR objects do support some kind of stream
interface, but I don't think they're accessible from Javascript.)

Note that the IFRAME + streamed SCRIPT method is only needed for web
apps that need to receive asynchronous messages from the server -
without continuously polling the server.  XHR is fine for apps which
only need to fetch something immediately following a user action.

It leaves me wondering if XHR should be extended to support streaming
properly, in a standard way...

Unfortunately the whole idea is quite at odds with the "official" way
HTTP and HTML are supposed to be used.  But then we lost that as soon
as the DOM become accessible, and people noticed you can make fancy
interactive applications.

-- Jamie

Received on Monday, 12 June 2006 19:34:06 UTC