Skip to main content
Streams
for the web
by @domenic
sources of streaming data
AJAX
web sockets
files
web workers
IndexedDB
web rtc

webcam
microphone
geolocation.watchPosition
the human finger
setInterval
web audio sources
readable streams

var flappyBird = readable.read();
sinks for streaming data
AJAX
web sockets
files
web workers
IndexedDB
web rtc

<audio>
<video>
<canvas>
<template>
writable streams

writable.write(flappyBird);
transformations of streaming data
CSV parser
the html parser
JSON to HTML via template
string encoding/decoding
audio/video codecs
encryption/decryption
gzip
web workers
transform streams

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

readable.pipeTo(writable);
complex pipe chains

readable
.pipeThrough(t1).pipeThrough(t2)
.pipeTo(writable);
the future
fetch("http://example.com/video.mp4")
.pipeThrough(new MP4DecoderStream())
.pipeThrough(specialEffectsWebWorker)
.pipeTo(document.query("video"));
the future
fetch("http://example.com/images.zip")
.pipeThrough(new Unzipper())
.pipeTo(new InfinitePhotoGallery(document.query("#gallery")));
the future
navigator.webcam
.pipeThrough(new AdaptiveVideoResizer())
.pipeTo(rtcPeerConnection);
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);
the present
FileReader
readAsBinaryString
onprogress
loaded
total
target
target.result
XMLHttpRequest
open
responseText
send
onreadystatechange
MessageChannel
port1
port2
start
close

Worker
getUserMedia

src
srcObject
URL.createObjectURL
onloadedmetadata
createMediaStreamSource
canvas

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

postMessage
onmessage
onerror
terminate
event.data
MediaStreamTrack
onstarted
onended
readystate
stop
URL.createObjectURL
why streams win
• They’re a unifying abstraction
• They separate concerns
• They encourage reusable code
why streams win
• They efficiently map to low-level OS primitives
• They avoid buffering in memory
• They encapsulate backpressure
https://github.com/whatwg/streams
understanding streams
push vs. pull sources
readable stream buffering
backpressure
high-water marks
demo time!
http://thlorenz.github.io/stream-viz/
writable stream buffering
congrats. that was the hard stuff.
can streams be generic?
sync vs. async
error handling
abort/cancel
E

E

E

E

abort!

C

abort!

cancel!

cancel!

E

C

E

E
abort!

cancel!

E

C

E
phew.
i guess … i guess that was the easy stuff?
why?
https://github.com/whatwg/streams