ReadableStreamDefaultController

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The ReadableStreamDefaultController interface of the Streams API represents a controller allowing control of a ReadableStream's state and internal queue. Default controllers are for streams that are not byte streams. 

Constructor

ReadableStreamDefaultController()
Creates and returns a ReadableStreamDefaultController() object instance.

Properties

ReadableStreamDefaultController.desiredSize
Returns the desired size required to fill the stream's internal queue.

Methods

ReadableStreamDefaultController.close()
Closes the associated stream.
ReadableStreamDefaultController.enqueue()
Enqueues a given chunk in the associated stream.
ReadableStreamDefaultController.error()
Causes any future interactions with the associated stream to error.

Examples

In the following simple example, a custom ReadableStream is created using a constructor (see our Simple random stream example for the full code). The start() function generates a random string of text every second and enqueues it into the stream. A cancel() function is also provided to stop the generation if ReadableStream.cancel() is called for any reason.

Note that a ReadableStreamDefaultController object is provided as the parameter of the start() and pull() functions.

When a button is pressed, the generation is stopped, the stream is closed using ReadableStreamDefaultController.close(), and another function is run, which reads the data back out of the stream.

const stream = new ReadableStream({
  start(controller) {
    interval = setInterval(() => {
      let string = randomChars();

      // Add the string to the stream
      controller.enqueue(string);

      // show it on the screen
      let listItem = document.createElement('li');
      listItem.textContent = string;
      list1.appendChild(listItem);
    }, 1000);

    button.addEventListener('click', function() {
      clearInterval(interval);
      fetchStream();
      controller.close();
    })
  },
  pull(controller) {
    // We don't really need a pull in this example
  },
  cancel() {
    // This is called if the reader cancels,
    // so we should stop generating strings
    clearInterval(interval);
  }
});

Specifications

Specification Status Comment
Streams
The definition of 'ReadableStreamDefaultController' in that specification.
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
Basic support
Experimental
Chrome Full support 52Edge ? Firefox Full support 57
Notes Disabled
Full support 57
Notes Disabled
Notes Not yet enabled by default. See bug 1389628.
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true) and the javascript.options.streams preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE ? Opera Full support 39Safari ? WebView Android Full support 52Chrome Android Full support 52Edge Mobile ? Firefox Android Full support 57
Notes Disabled
Full support 57
Notes Disabled
Notes Not yet enabled by default. See bug 1389628.
Disabled From version 57: this feature is behind the dom.streams.enabled preference (needs to be set to true) and the javascript.options.streams preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support 39Safari iOS ? Samsung Internet Android ?
ReadableStreamDefaultController() constructor
Experimental
Chrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
close
Experimental
Chrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
desiredSize
Experimental
Chrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
enqueue
Experimental
Chrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?
error
Experimental
Chrome ? Edge ? Firefox ? IE ? Opera ? Safari ? WebView Android ? Chrome Android ? Edge Mobile ? Firefox Android ? Opera Android ? Safari iOS ? Samsung Internet Android ?

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

Document Tags and Contributors

Contributors to this page: fscholz, chrisdavidmills
Last updated by: fscholz,