Draft
This page is not complete.
This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The Streams API allows JavaScript to programmatically access streams of data received over the network and process them as desired by the developer.
Streaming involves breaking a resource that you want to receive over a network down into small chunks, then processing it bit by bit. This is something browsers do anyway when receiving assets to be shown on webpages — videos buffer and more is gradually available to play, and sometimes you'll see images display gradually as more is loaded.
But this has never been available to JavaScript before. Previously, if we wanted to process a resource of some kind (be it a video, or a text file, etc.), we'd have to download the entire file, wait for it to be deserialized into a suitable format, then process the whole lot after it is fully received.
With Streams being available to JavaScript, this all changes — you can now start processing raw data with JavaScript bit by bit as soon as it is available on the client-side, without needing to generate a buffer, string, or blob.
There are more advantages too — you can detect when streams start or end, chain streams together, handle errors and cancel steams as required, and react to the speed the stream is being read at.
The basic usage of Streams hinges around making reponses available as streams. For example, the response Body
returned by a successful fetch request can be exposed as a ReadableStream
, and you can then read it using a reader created with ReadableStream.getReader()
, cancel it with ReadableStream.cancel()
, etc.
More complicated uses involve creating your own stream using the ReadableStream.ReadableStream()
constructor, for example to process data inside a service worker.
You can also write data to streams using WritableStream
.
Note: You can find a lot more details about the theory and practice of streams in our articles — Streams API concepts, Using readable streams, and Using writable streams.
ReadableStream
ReadableStream.ReadableStream()
constructor).ReadableStreamDefaultReader
ReadableStreamBYOBReader
ReadableStream.ReadableSteam()
constructor).ReadableStreamDefaultController
ReadableStream
's state and internal queue. Default controllers are for streams that are not byte streams.ReadableByteStreamController
ReadableStream
's state and internal queue. Byte stream controllers are for byte streams.ReadableStreamBYOBRequest
ReadableByteStreamController
.WritableStream
WritableStreamDefaultWriter
WritableStreamDefaultController
WritableStream
's state. When constructing a WritableStream
, the underlying sink is given a corresponding WritableStreamDefaultController
instance to manipulate.ByteLengthQueuingStrategy
CountQueuingStrategy
Request
Request
object is constructed, you can pass it a ReadableStream
in the body
property of its RequestInit
dictionary. This Request
could then be passed to a WindowOrWorkerGlobalScope.fetch()
to commence fetching the stream.Body
Body
returned by a successful fetch request is exposed by default as a ReadableStream
, and can have a reader attached to it, etc.We have created a directory of examples to go along with the Streams API documentation — see mdn/dom-examples/streams. The examples are as follows:
pipeThrough()
can be used to transform a ReadableStream into a stream of other data types by transforming a data of a PNG file into a stream of PNG chunks.Specification | Status | Comment |
---|---|---|
Streams | Living Standard | Initial definition. |
No compatibility data found. Please contribute data for "path.to.feature.NameOfAPI" (depth: 1) to the MDN compatibility data repository.
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Streams_API