Readable Stream
readable stream methods
Anything that you can call .pipe()
on.
stream.pipe(...)
stream.once('end', function () {})
☛ can listen to theend
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) {})
☛ thedata
flag kicks off the old mode (stream version 1). This is a hack for compatibility.