Node.js streams 101

Michał Michalczuk

michalczukm.xyz

Michał Michalczuk

Senior JavaScript Developer

Ciklum / Stibo Systems

 

 

IT trainer

infoShare Academy

Node.js streams 101

Michał Michalczuk

michalczukm.xyz

  • Single threaded
  • Avoid computation
     
  • Delegate to I/O
  • Write as async as possible

How does streams

fit in?

What are streams?

Is it new idea?

And what is the problem?

The problem

Streams, streams everywhere

streams you know

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

streams you know

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
http.IncommingMessage
Readible Stream
http.ServerResponse
Writable Stream

streams you know

process.stdin



process.stdout
Readible Stream
Writable Stream
fs.createReadStream(...)



fs.createWriteStream(...)
Readible Stream
Writable Stream

Text

ls | wc

Output

Input

Pipe

ls | wc
process.stdin.pipe(process.stdout);

Readable stream

Writable stream

          .pipe(        );

Streams types

Readible
Writable
Duplex
Transform

Streams types

Readible
Writable
Duplex
Transform

Streams types

Writable
Transform
const { Writable } = require('stream');
const outStream = new Writable({
  write(chunk, encoding, callback) {
    console.log(chunk.toString().toUpperCase());
    callback();
  }
});

process.stdin.pipe(outStream);

Streams types

Readable
Transform
const { Readable } = require('stream'); 
const inStream = new Readable({
  read() {
  }
});
inStream.push('first chunk\n');
inStream.push('sedond chunk\n');
inStream.push(null);

setTimeout(() => {
    inStream.pipe(process.stdout);
}, 2000);

Streams types

Duplex
Transform
const { Duplex } = require('stream');

const inoutStream = new Duplex({
  write(chunk, encoding, callback) {
    console.log(chunk.toString().toUpperCase());
    callback();
  },

  read(size) {
  }
});

inoutStream.push('first chunk\n');
inoutStream.push('second chunk\n');
inoutStream.push(null);

process.stdin.pipe(inoutStream).pipe(process.stdout);

Streams types

Transform
Transform
const { Transform } = require('stream');

const upperCaseTr = new Transform({
  transform(chunk, encoding, callback) {
    this.push(chunk.toString().toUpperCase());
    callback();
  }
});

process.stdin.pipe(upperCaseTr).pipe(process.stdout);

Why

 

 

 

streams are important

and where to use them?

 

Take a look at it

Short summary

Michał Michalczuk

michalczukm.xyz

Thank you!

michalczukm

Questions time

Michał Michalczuk

michalczukm.xyz

Thank you!

michalczukm

Node.js streams 101

By Michał Michalczuk

Node.js streams 101

No matter what you are doing as front-end developer Node.js is everywhere and you won’t escape from it. On my presentation I will focus on steams - one of the basic, and often not understand, element of this environment. How they work and what benefit you will get by using them as intended.

  • 97
Loading comments...

More from Michał Michalczuk