Nodejs streams 101

Because everything is a stream

Streams

In modern web application almost everything can be seen as a stream

Actions

Users click on the UI, perform actions, input data, ask the server for data

Stream types

there's more than one you know :)

Stream Types

  • Write
  • Read
  • Transform/Duplex

Stream Types

It's a stream you can write to:

You have met this before for sure:

router.use('/index', (req, res, next) => {

  res.send('HI')

  //in the source turns into

  //res.end('HI', encoding)<---- there you go, streams :)

}

Write

Stream Types

On WriteStreams you can `write` or `end`

Write

Streams use events

  • Read: close data end error readable
  • Write: close drain error finish drain pipe unpipe
  • Transform: both of the above

Streams use events

  • Read: close data end error readable
  • Write: close drain error finish drain pipe unpipe
  • Transform: both of the above

Yes:

Write stream has a finish event  

Read has an end event

¯\_ツ_/¯

Streams manage data

basically binary data

There are helpers though

and very good libraries

"Raw" streams

  • Hard to get working the way you want them to
  • Hard to combine efficiently

"Raw" streams

  • Hard to get working the way you want them to
  • Hard to combine efficiently

Enter `mississippi`

A lot of useful helpers

A lot of useful helpers

Nodejs Streams

By marcoalbarelli

Nodejs Streams

  • 422
Loading comments...

More from marcoalbarelli