Streams for every day

December 03, 2015

“Streams in node are one of the rare occasions when doing something the fast way is actually easier. SO USE THEM. not since bash has streaming been introduced into a high level language as nicely as it is in node.” @dominictarr at high level node style guide.

TL;DR

tweetStream.pipe(process.stdout)

Readable

Streams from which data can be read (e.g fs.createReadStream()).

const toReadableStream = input => (
  new Readable({
    read () {
      this.push(input)
      this.push(null)
    }
  })
)

Writable

Streams to which data can be written (e.g fs.createWriteStream()).

Duplex

Streams that are both Readable and Writable. Both are independent and each have separate internal buffer. (e.g net.Socket).

                             Duplex Stream
                          ------------------|
                    Read  <-----               External Source
            You           ------------------|   
                    Write ----->               External Sink
                          ------------------|
            You don't get what you write. It is sent to another source.

Transform

Duplex streams where the output is in some way related to the input (e.g zlib streams).

                                 Transform Stream
                           --------------|--------------
            You     Write  ---->                   ---->  Read  You
                           --------------|--------------
            You write something, it is transformed, then you read something.

File System/Descriptor Streams

They are a subclass of Readable/Writable streams because they interact with the filesystem, emitting special kind of events

Child Process

What about Callback

Bonus Extra

Interested libraries to use with streams are:

Bibliography

Kiko Beats

Kiko Beats