jpillora / xdomain

A pure JavaScript CORS alternative
https://jpillora.com/xdomain/
3.12k stars 270 forks source link

Feature: Streaming responses #96

Open peterjosling opened 9 years ago

peterjosling commented 9 years ago

The XHR emits progress events as per the spec, but doesn't populate the response/responseText properties until the request has completed, so it's impossible to utilise streaming requests (for things such as Twitter's streaming API).

jpillora commented 9 years ago

Very true, response/text is not set per progress.

As an aside, I briefly tested Twitter's API with XMLHttpRequest and it wasn't very fun, had to pull the current text and diff it with what I had already processed. This would get slower and slower the longer you stream. Have you also encountered this?

peterjosling commented 9 years ago

Previously I've just stored the length of the response body from the previous progress event and then pulled out the substring from that index onwards. Speed's never been an issue - I just put in a check to close and reopen the stream if the response grew over a few MB in size (as you can't clear it).

jpillora commented 9 years ago

Ah yep, length slicing + reopening the stream seems like a good solution. This shouldn't be too hard to add, will look into it tonight

mscdex commented 9 years ago

+1 This is what Yaffle/EventSource does and it would be great to have these two polyfills work together better.