node.js streams talk

25
ROW ROW ROW YOUR BOA T by @zladuric

Upload: zladuric

Post on 30-Jun-2015

521 views

Category:

Internet


0 download

DESCRIPTION

Node.js talk on streams

TRANSCRIPT

Page 1: Node.js streams talk

ROW ROW ROW YOUR BOAT

by @zladuric

Page 2: Node.js streams talk

We have gathered here today

● How to build Node.js Streams?

● A talk to introduce the topic of streams inJavaScript, how this works in Node.js and howit didn't work out before.

● You're my lab!

by @zladuric

Page 3: Node.js streams talk

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

Page 4: Node.js streams talk

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 reuseparts we got

● PIPES!!1

Page 5: Node.js streams talk

The past

Let's hope it dies!

Page 6: Node.js streams talk

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 transferwith Flash, Java Applets

On the server● Comes largely from unix

network sockets

● stdin, stdout, stderr

● Created to streamline (yeah, Iknow) I/O operations

● In web: Flash Media server, Red5

● Encoders/decoders, format converters● Bridges between backend services, more I/O

WTF?

Page 7: Node.js streams talk

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 ornot

Page 8: Node.js streams talk

The present

Node.js “streams2”:

Stability: 2 – Unstable

● Node.js is unstable ;)

● No breaking changes expected for Node.js 0.12

Page 9: Node.js streams talk

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

Page 10: Node.js streams talk

How to be streamin' in Node

$(#button).on('click', handler); anyone?

Dumb example

Page 11: Node.js streams talk

stream.Readable

● Flowing and non-flowing?

● Common events and methods

● Examples: fs.createReadStream,http.createServer listeners, tcp and net sockets.

Page 12: Node.js streams talk

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.”

Page 13: Node.js streams talk

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

Page 14: Node.js streams talk

Borin Simple example

● this.push(); → stream.read();

● objectMode is handy

Page 15: Node.js streams talk

Writable stream

● To implement a writable stream, implement_write method, drain

Page 16: Node.js streams talk

Transform streams

Splits input intolines

● _transform() - whatto do on input data

● _flush – what to dowhen input ends

Page 17: Node.js streams talk

And now pipes!

var stream = getSomeReadStream()

.pipe(toConverterStream())

.pipe(toEncoderStream())

.pipe(toTransportStream())

.pipe(toTheConsumerStream());

Page 18: Node.js streams talk

Piping examples

Read file line by line Top secret

Steal Google logo

Page 19: Node.js streams talk

Do you use it?

Page 20: Node.js streams talk

The client

● WebRTC, WebSockets, Workers – they allstream data

● XMLHttpRequest

● User Media (MediaStream) API,Blob/FileSystem API, Audio, Geolocation etc.

Page 21: Node.js streams talk

Example

http://jszgb.com/ - hacked to steal selfies

<video id="video" autoplay></video>

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

Page 22: Node.js streams talk

Other useful options, events andmethods

● 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

Page 23: Node.js streams talk

The future

● Node.js is relatively stable

● Whatwg is trying to standardize stream APIs

from: Domenic Denicola

Page 24: Node.js streams talk

Play time

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

Page 25: Node.js streams talk

Question time

1. Was my english ok?

2. How boring was I, from 1 – 10?

Your turn.

Zlatko Đurić