My FeedDiscussionsHeadless CMS
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

How to use Node's stream module

Yanni Nightingale's photo
Yanni Nightingale
·Oct 10, 2016

As the definition of Node.js, A stream is an abstract interface for working with streaming data in Node.js. That means you CANNOT construct a stream object directly, but implement it. Streams can be readable, writable, both(duplex) or transformable.

Implement a stream

Readable stream

Extend stream.Readable to implement a readable stream. The only method you have to implement is _read():

class MyReadable extends Readable {
  constructor(options) {
    super(options);
  }
  _read(size) {
  }
}

The __read()_ method don't have to return anything. Data being read is push to a internal buffer:

  _read(size) {
    this.push('xyz');// or this.push(Buffer.from('xyz'))
  }

If there is nothing to read, just push a null:

this.push(null);

E.g. alphabet-readable-stream.js

Writable stream

Extend stream.Writable to implement a writable stream. The method you have to implement is _write() or _writev():

const Writable = require('stream').Writable;

class MyWritable extends Writable {
  constructor(options) {
    super(options);
  }
  _write(chunk, encoding, callback) {
    callback();
  }
}

Call callback() function to represent a complete. Passing an error object is optional.

__writev() acts as a bulk of _writev()_ somehow.

E.g. arraywriter-writable-stream.js

Duplex stream

A duplex stream is a mixin of readable stream and writable stream, but extending stream.Duplex.

Transform stream

A transform stream is a kind of duplex stream that the output is in some way related to the input. The only method you have to implement is _transform():

const Transform = require('stream').Transform;

class MyTransform extends Transform {
  constructor(options) {
    super(options);
  }
  _transform(chunk, encoding, callback) {
  }
}

__transform() is similar to _write()_.

Another method you can implement is __flush()_:

  _flush(cb) {
    this.push('\n');
    cb();
  }

E.g. reverse-transform-stream.js

Object mode stream

In the examples above, we read, write or transform strings and buffers. Actually, objects can be read, wrote and transformed too. Pass a objectMode to constructor:

new MyReadable({objectMode: true});

E.g. object-readable-stream.js E.g. object-writable-stream.js

through2

To simplify the procedure of creating a duplex stream, through2 is highly recommended.