streams for the web

38
Streams for the web by @domenic

Upload: domenic-denicola

Post on 08-May-2015

4.239 views

Category:

Technology


0 download

DESCRIPTION

Streams are a fundamental programming primitive for representing the flow of data through your system. It's time we brought this powerful tool to the web. What if we could stream data from a HTTP request, through a web worker that transforms it, and then into a tag? Over the last year, I've been working on the WHATWG streams specification, which builds upon the lessons learned in Node.js, to provide a suitable abstraction for needs of the extensible web. I'll discuss briefly why streams are important, what they enable, and the role we envision them playing in the future of the web platform. Mostly, though, I want to help you understand streams, at a deep level. In the course of writing this specification, I've learned a lot about streams, and I want to share that knowledge with you. At the core, they are a very simple and beautiful abstraction. I think we've done a good job capturing that abstraction, and producing an API the web can be proud of. I'd love to tell you all about it.

TRANSCRIPT

Page 1: Streams for the Web

Streamsfor the web

by @domenic

Page 2: Streams for the Web
Page 3: Streams for the Web
Page 4: Streams for the Web
Page 5: Streams for the Web
Page 6: Streams for the Web

sources of streaming data

AJAXweb sockets

filesweb workersIndexedDB

web rtc

webcammicrophone

geolocation.watchPositionthe human finger

setIntervalweb audio sources

Page 7: Streams for the Web

readable streams

var flappyBird = readable.read();

Page 8: Streams for the Web

sinks for streaming data

AJAXweb sockets

filesweb workersIndexedDB

web rtc

<audio><video>

<canvas><template>

Page 9: Streams for the Web

writable streams

writable.write(flappyBird);

Page 10: Streams for the Web

transformations of streaming data

CSV parserthe html parser

JSON to HTML via templatestring encoding/decoding

audio/video codecsencryption/decryption

gzipweb workers

Page 11: Streams for the Web

transform streams

transform.input.write(flappyBird);var evilBird = transform.output.read();

Page 12: Streams for the Web

simple pipe chains

readable.pipeTo(writable);

Page 13: Streams for the Web

complex pipe chains

readable .pipeThrough(t1).pipeThrough(t2) .pipeTo(writable);

Page 14: Streams for the Web

the future

fetch("http://example.com/video.mp4") .pipeThrough(new MP4DecoderStream()) .pipeThrough(specialEffectsWebWorker) .pipeTo(document.query("video"));

Page 15: Streams for the Web

the future

fetch("http://example.com/images.zip") .pipeThrough(new Unzipper()) .pipeTo(new InfinitePhotoGallery(document.query("#gallery")));

Page 16: Streams for the Web

the future

navigator.webcam .pipeThrough(new AdaptiveVideoResizer()) .pipeTo(rtcPeerConnection);

Page 17: Streams for the Web

the future

var ws = new WebSocket("http://example.com/analytics");var events = new EventStream(document, "click");

events .pipeThrough(new EventThrottler()) .pipeThrough(new EventsAsJSON()) .pipeTo(ws.input);

Page 18: Streams for the Web

the present

FileReader

readAsBinaryString onprogress loaded total target target.result

XMLHttpRequest

open responseText send onreadystatechange

MessageChannel

port1 port2 start close

getUserMedia

src srcObject URL.createObjectURL onloadedmetadata createMediaStreamSource

canvas

getContext('2d').drawImage toDataURL createElement appendChild querySelector

Worker

postMessage onmessage onerror terminate event.data

MediaStreamTrack

onstarted onended readystate stop URL.createObjectURL

Page 19: Streams for the Web

why streams win

•They’re a unifying abstraction•They separate concerns•They encourage reusable code

Page 20: Streams for the Web

why streams win

•They efficiently map to low-level OS primitives•They avoid buffering in memory•They encapsulate backpressure

Page 21: Streams for the Web

https://github.com/whatwg/streams

Page 22: Streams for the Web

understanding streams

Page 23: Streams for the Web

push vs. pull sources

Page 24: Streams for the Web

readable stream buffering

Page 25: Streams for the Web

backpressure

Page 26: Streams for the Web

high-water marks

Page 27: Streams for the Web

demo time!

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

Page 28: Streams for the Web

writable stream buffering

Page 29: Streams for the Web

congrats. that was the hard stuff.

Page 30: Streams for the Web

can streams be generic?

Page 31: Streams for the Web

sync vs. async

Page 32: Streams for the Web

error handling

Page 33: Streams for the Web

abort/cancel

Page 34: Streams for the Web

E E E E E Eabort! abort! abort!

C E C E C E

cancel!cancel!cancel!

Page 35: Streams for the Web

phew.i guess … i guess that was the easy stuff?

Page 36: Streams for the Web

why?

Page 37: Streams for the Web
Page 38: Streams for the Web

https://github.com/whatwg/streams