Readable Stream

In notebook:
FrontEndMasters Networking and Streams
Created at:
2017-09-24
Updated:
2017-09-24
Tags:
backend Node JS JavaScript Fundamentals

readable stream methods

Anything that you can call .pipe() on.

  • stream.pipe(...)
  • stream.once('end', function () {}) ☛ can listen to the end

you probably won't need to call these very often:

  • stream.read()
  • stream.on('readable', function () {})

You usually don't call these manually yourself, he suggest that

you can let a module or .pipe() take care of calling those

  const fs = require('fs')
var r = fs.createReadStream(process.argv[2])

r.pipe(process.stdout)

There are two modes to readable streams, paused and flowing.

readable: paused mode

default behavior with automatic backpressure

They only going to produce data, when you are consuming data. If we want to stop consuming data, all we have to do is stop calling next(). NodeJS introduced this in streams2. This is an automatic way to handle backpressure. In TCP you have windowing where the consumer can inform the producer to stop sending data. NodeJS will do this relatively automatically.


readable: flowing mode

this is the older mode. data is consumed as soon as chunks are available (no backpressure) Can be useful in a non-production setting to consume a stream easily. Or when you would buffer up the data anyways, so it doesn't really matter. But generally you don't want to...

turn on flowing mode with:

  • stream.resume()
  • stream.on('data', function (buf) {}) ☛ the data flag kicks off the old mode (stream version 1). This is a hack for compatibility.