Nodejs streams

From wikinotes
Revision as of 03:10, 6 August 2021 by Will (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The official docs are very good here, this is just quick overview.

Documentation

official docs https://nodejs.org/api/stream.html
official tutorial https://nodejs.dev/learn/nodejs-streams

Usage

// pipelines
stream1
  .pipe(stream2)
  .pipe(stream3)

Stream Types

Readable

// readable-event, like select
stream.on('readable', () => { console.log(stream.read()) })

// pipe readable to writable
stream.pipe(writableStream)

// write to readable stream
stream.push('hi')

Writable

stream.write('abc')  // write
stream.end()         // nothing left to write

Duplex

Transform