Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @microsoft/stream-collator with all npm packages installed. Try it out:

var streamCollator = require("@microsoft/stream-collator")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@microsoft/stream-collator v3.2.4

Display intelligible realtime output from your asynchronous streams


This library enables a tool to display live console output from multiple asynchronous processes, while ensuring that their output does not get jumbled together.

How does it work?

The stream-collator manages the output of these streams, ensuring that no two streams are writing to the console at the same time. At any given time, one stream registered with the collator is the active stream, which means that particular stream will be live streaming, while the others will wait for that stream to finish before their output is displayed.

For example, if you have 3 streams (e.g. from using child_process.spawn()).

Stream A will write: AAAAA


Stream C will write: CCCCCCCCCC

If these streams are all being piped directly to stdout (without stream-collator), you could end up with jumbled output:


Something like the following would be much more useful to users of your application:


This is where the stream-collator comes in!


Install the stream-collator:

npm install --save @microsoft/stream-collator

Import the collator:

import StreamCollator from '@microsoft/stream-collator'; // es6
const StreamCollator = require('@microsoft/stream-collator'); // commonjs

A stream collator adheres to the NodeJS Stream API, meaning that it effectively is special type of ReadableStream. This makes working with the stream collator very simple. Imagine we had the 3 streams from the example above:

const streamA = getRepeaterStream('A', 5); // fake helper function that returns a ReadableStream
const streamB = getRepeaterStream('B', 15); // fake helper function that returns a ReadableStream
const streamC = getRepeaterStream('C', 10); // fake helper function that returns a ReadableStream

Next, instantiate a stream collator instance and register the streams with it:

const collator = new StreamCollator();


collator is now a stream which can be accessed with the standard stream API's. For example, you could pass the output to process.stdout:


Or a file:

var wstream = fs.createWriteStream('myOutput.txt');


The active stream

At any given time, a single stream is designated as the active stream. The output of the active stream will always be live-streamed. This is particularly useful for long-running streams. When the active stream finishes, a new stream is selected as the active stream and all of its contents up to that point will be emitted. Whenever an active stream finishes, all background streams which have been completed will be emitted.

Helper streams

Two additional stream classes are also exported with this package:


A utility string-based stream with two sub-streams, stdout and stderr. These streams can be written to, and will be emitted by this class. Anything written to stderr will be automatically wrapped in red coloring, unless is begins with the text Warning -, in which case it will write the warning to stdout in yellow.


A special string-based stream with a function readAll() which will return the contents of everything that has been written to the stream as a string, regardless of whether the stream is open or closed.


NOTE: Ending the collator stream could be improved with an option that lets you select between the following behaviors:

  • Close the collator stream when ANY registered stream has been closed
  • Close the collator stream when ALL registered streams have been closed
  • Don't automatically close the collator stream
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free