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?

Michał
Michalczuk

@michalczukm

Senior Software Engineer

Spartez / Atlassian

IT trainer

infoShare Academy

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}/`);
});
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
ls | wc

Output

Input

Pipe

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

Readable stream

Writable stream

          .pipe(        );
process.stdin.pipe(wc);

Examples

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

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

process.stdin.pipe(outStream);
Readable
Transform
const { Readable } = require('stream'); 

const inStream = new Readable();
inStream.push('first chunk\n');
inStream.push('second chunk\n');
inStream.push(null);

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

Streams types

Readable

Writable

Dup lex

Trans form

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);
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);

 

 

 

 

where to use streams?

Real-life examples

Short summary

Links

Questions time

Feedback?

Yes please

Michał Michalczuk

michalczukm.xyz

Thank you!

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.

  • 386
Loading comments...

More from Michał Michalczuk