Slide 1

Slide 1 text

ROW ROW ROW YOUR BOAT by @zladuric

Slide 2

Slide 2 text

We have gathered here today ● How to build Node.js Streams? ● A talk to introduce the topic of streams in JavaScript, how this works in Node.js and how it didn't work out before. ● You're my lab! by @zladuric

Slide 3

Slide 3 text

What are we talking about ● The past - what was there before ● The present - what are we doing now ● The future - how you can get and use this ● The client - the browser side of things ● The server - Streams API

Slide 4

Slide 4 text

But first - why a stream Regular stuff ● Comes as a whole blob ● Takes up memory ● When action is broken at 95%, redo the whole thing Streamed stuff ● Just like regular, but chunked into bits ● When broken, we can still reuse parts we got ● PIPES!!1

Slide 5

Slide 5 text

The past Let's hope it dies!

Slide 6

Slide 6 text

All right, I'll tell ya On the client ● Until a few years ago, we could not do much with streams in JS ● Mostly for video/audio transfer with Flash, Java Applets On the server ● Comes largely from unix network sockets ● stdin, stdout, stderr ● Created to streamline (yeah, I know) I/O operations ● In web: Flash Media server, Red5 ● Encoders/decoders, format converters ● Bridges between backend services, more I/O WTF?

Slide 7

Slide 7 text

Node.js past ● Streams API was (is?) very volatile ● Backwards compatibility ● Main problems: – Your stream could ignore stream.pause();) – Stream starts right away – whether you're ready or not

Slide 8

Slide 8 text

The present Node.js “streams2”: Stability: 2 – Unstable ● Node.js is unstable ;) ● No breaking changes expected for Node.js 0.12

Slide 9

Slide 9 text

Stream in Node.js ● All streams are instances of an EventEmmiter (events and listeners/hooks) ● var Readable = require('stream').Readable; – Implement your own _read(); ● var Writable = require('stream').Writable; – Implement your own _write(); ● var Duplex = implement both; ● var Transform = convert input then pass to output – Implement _transform(); and flush();

Slide 10

Slide 10 text

How to be streamin' in Node $(#button).on('click', handler); anyone?

Slide 11

Slide 11 text

stream.Readable ● Flowing and non-flowing? ● Common events and methods ● Examples: fs.createReadStream, http.createServer listeners, tcp and net sockets.

Slide 12

Slide 12 text

Translate stream events into english When stream says: It means: ● readable “I have stuff to be read.” ● data “Here's a chunk.” ● end “I'm all out, you've read it.” ● close “Bye bye!” (file.close); ● error “Pain starts here.”

Slide 13

Slide 13 text

Pushmi-pullyu Flowing mode stream.on('data', function(chunk) { // chunk.length bytes // or if we set encoding, chunk is a string // or if objectMode: true, then values/objs ); Non-flowing: var chunk = stream.read();

Slide 14

Slide 14 text

Borin Simple example ● this.push(); → stream.read(); ● objectMode is handy

Slide 15

Slide 15 text

Writable stream ● To implement a writable stream, implement _write method, drain

Slide 16

Slide 16 text

Transform streams Splits input into lines ● _transform() - what to do on input data ● _flush – what to do when input ends

Slide 17

Slide 17 text

And now pipes! var stream = getSomeReadStream() .pipe(toConverterStream()) .pipe(toEncoderStream()) .pipe(toTransportStream()) .pipe(toTheConsumerStream());

Slide 18

Slide 18 text

Piping examples Read file line by line Top secret Steal Google logo

Slide 19

Slide 19 text

Do you use it?

Slide 20

Slide 20 text

The client ● WebRTC, WebSockets, Workers – they all stream data ● XMLHttpRequest ● User Media (MediaStream) API, Blob/FileSystem API, Audio, Geolocation etc.

Slide 21

Slide 21 text

Example http://jszgb.com/ - hacked to steal selfies // simplified navigator.webkitGetUserMedia({video: true}, function(localMediaStream) { console.log('getting some video'); var video = document.getElementById('video'); video.src = window.URL.createObjectURL(localMediaStream); }, console.log);

Slide 22

Slide 22 text

Other useful options, events and methods ● options.highWaterMark = 1024; ● stream.setEncoding('utf-8'); ● pause/resume a stream ● wrap(); - for old libs < Node v0.10 ● pipe()/unpipe(), unshift() ● 'finish', 'close', 'drain' events

Slide 23

Slide 23 text

The future ● Node.js is relatively stable ● Whatwg is trying to standardize stream APIs from: Domenic Denicola

Slide 24

Slide 24 text

Play time http://thlorenz.github.io/stream-viz/

Slide 25

Slide 25 text

Question time 1. Was my english ok? 2. How boring was I, from 1 – 10? Your turn. Zlatko Đurić