docs.rodeo

MDN Web Docs mirror

Streams API

{{DefaultAPISidebar("Streams")}} {{AvailableInWorkers}} 

The Streams API allows JavaScript to programmatically access streams of data received over the network and process them as desired by the developer.

Concepts and usage

Streaming involves breaking a resource that you want to receive over a network down into small chunks, then processing it bit by bit. Browsers already do this when receiving media assets — videos buffer and play as more of the content downloads, and sometimes you’ll see images display gradually as more is loaded too.

But this capability has never been available to JavaScript before. Previously, if we wanted to process a resource of some kind (video, text file, etc.), we’d have to download the entire file, wait for it to be deserialized into a suitable format, then process all the data.

With the Streams API, you can start processing raw data with JavaScript bit by bit, as soon as it is available, without needing to generate a buffer, string, or blob.

The basic concept of the stream API is data is fetched from the network in several data packets. The data is processed, and then sent to the browser in a stream of data packets.

There are more advantages too — you can detect when streams start or end, chain streams together, handle errors and cancel streams as required, and react to the speed at which the stream is being read.

The usage of Streams hinges on making responses available as streams. For example, the response body returned by a successful fetch request is a {{domxref("ReadableStream")}}  that can be read by a reader created with {{domxref("ReadableStream.getReader()")}} .

More complicated uses involve creating your own stream using the {{domxref("ReadableStream.ReadableStream", "ReadableStream()")}}  constructor, for example to process data inside a service worker.

You can also write data to streams using {{domxref("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, Using readable byte streams, and Using writable streams.

Stream interfaces

Readable streams

Writable streams

Transform Streams

Extensions to other APIs

Examples

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:

Examples from other developers:

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN