狠狠撸

狠狠撸Share a Scribd company logo
ROW ROW ROW YOUR BOAT 
by @zladuric
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
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
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
The past 
Let's hope it dies!
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?
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
The present 
Node.js “streams2”: 
Stability: 2 – Unstable 
● Node.js is unstable ;) 
● No breaking changes expected for Node.js 0.12
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();
How to be streamin' in Node 
$(#button).on('click', handler); anyone? 
Dumb example
stream.Readable 
● Flowing and non-flowing? 
● Common events and methods 
● Examples: fs.createReadStream, 
http.createServer listeners, tcp and net sockets.
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.”
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();
Borin Simple example 
● this.push(); → stream.read(); 
● objectMode is handy
Writable stream 
● To implement a writable stream, implement 
_write method, drain
Transform streams 
Splits input into 
lines 
● _transform() - what 
to do on input data 
● _flush – what to do 
when input ends
And now pipes! 
var stream = getSomeReadStream() 
.pipe(toConverterStream()) 
.pipe(toEncoderStream()) 
.pipe(toTransportStream()) 
.pipe(toTheConsumerStream());
Piping examples 
Read file line by line Top secret 
Steal Google logo
Do you use it?
The client 
● WebRTC, WebSockets, Workers – they all 
stream data 
● XMLHttpRequest 
● User Media (MediaStream) API, 
Blob/FileSystem API, Audio, Geolocation etc.
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 = 
/slideshow/nodejs-streams-talk/39693897/window.URL.createObjectURL(localMediaStream); 
}, console.log); 
</script>
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
The future 
● Node.js is relatively stable 
● Whatwg is trying to standardize stream APIs 
from: Domenic Denicola
Play time 
http://thlorenz.github.io/stream-viz/
Question time 
1. Was my english ok? 
2. How boring was I, from 1 – 10? 
Your turn. 
Zlatko ?uri?

More Related Content

What's hot (20)

Not only SQL
Not only SQL Not only SQL
Not only SQL
Niklas Gustavsson
?
Node js crash course session 2
Node js crash course   session 2Node js crash course   session 2
Node js crash course session 2
Abdul Rahman Masri Attal
?
REST vs SOAP
REST vs SOAPREST vs SOAP
REST vs SOAP
javicid
?
P99CONF — What We Need to Unlearn About Persistent Storage
P99CONF — What We Need to Unlearn About Persistent StorageP99CONF — What We Need to Unlearn About Persistent Storage
P99CONF — What We Need to Unlearn About Persistent Storage
ScyllaDB
?
Big mountain data competition training: scraping-n-munge
Big mountain data competition training: scraping-n-mungeBig mountain data competition training: scraping-n-munge
Big mountain data competition training: scraping-n-munge
David "Gonzo" Gonzalez
?
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
MongoDB
?
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Dvir Volk
?
Node.js and Cassandra
Node.js and CassandraNode.js and Cassandra
Node.js and Cassandra
Stratio
?
PHP Streams: Lucky Dip
PHP Streams: Lucky DipPHP Streams: Lucky Dip
PHP Streams: Lucky Dip
Wez Furlong
?
Cassandra Anti-Patterns
Cassandra Anti-PatternsCassandra Anti-Patterns
Cassandra Anti-Patterns
Matthew Dennis
?
PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)
Aleksander Alekseev
?
MongoFr : MongoDB as a log Collector
MongoFr : MongoDB as a log CollectorMongoFr : MongoDB as a log Collector
MongoFr : MongoDB as a log Collector
Pierre Baillet
?
strangeloop 2012 apache cassandra anti patterns
strangeloop 2012 apache cassandra anti patternsstrangeloop 2012 apache cassandra anti patterns
strangeloop 2012 apache cassandra anti patterns
Matthew Dennis
?
Update on Crimson - the Seastarized Ceph - Seastar Summit
Update on Crimson  - the Seastarized Ceph - Seastar SummitUpdate on Crimson  - the Seastarized Ceph - Seastar Summit
Update on Crimson - the Seastarized Ceph - Seastar Summit
ScyllaDB
?
Invitation to mongo db @ Rakuten TechTalk
Invitation to mongo db @ Rakuten TechTalkInvitation to mongo db @ Rakuten TechTalk
Invitation to mongo db @ Rakuten TechTalk
Ryuji Tamagawa
?
Difference between php and node
Difference between php and nodeDifference between php and node
Difference between php and node
Nguy?n Ng?c Ph??ng
?
Cassandra On EC2
Cassandra On EC2Cassandra On EC2
Cassandra On EC2
Matthew Dennis
?
Avoiding Data Hotspots at Scale
Avoiding Data Hotspots at ScaleAvoiding Data Hotspots at Scale
Avoiding Data Hotspots at Scale
ScyllaDB
?
Embulk and Machine Learning infrastructure
Embulk and Machine Learning infrastructureEmbulk and Machine Learning infrastructure
Embulk and Machine Learning infrastructure
Hiroshi Toyama
?
Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...
Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...
Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...
DataStax Academy
?
REST vs SOAP
REST vs SOAPREST vs SOAP
REST vs SOAP
javicid
?
P99CONF — What We Need to Unlearn About Persistent Storage
P99CONF — What We Need to Unlearn About Persistent StorageP99CONF — What We Need to Unlearn About Persistent Storage
P99CONF — What We Need to Unlearn About Persistent Storage
ScyllaDB
?
Big mountain data competition training: scraping-n-munge
Big mountain data competition training: scraping-n-mungeBig mountain data competition training: scraping-n-munge
Big mountain data competition training: scraping-n-munge
David "Gonzo" Gonzalez
?
Mastering the MongoDB Shell
Mastering the MongoDB ShellMastering the MongoDB Shell
Mastering the MongoDB Shell
MongoDB
?
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
Dvir Volk
?
Node.js and Cassandra
Node.js and CassandraNode.js and Cassandra
Node.js and Cassandra
Stratio
?
PHP Streams: Lucky Dip
PHP Streams: Lucky DipPHP Streams: Lucky Dip
PHP Streams: Lucky Dip
Wez Furlong
?
PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)PostgreSQL and Compressed Documents (pgconf.ru 2018)
PostgreSQL and Compressed Documents (pgconf.ru 2018)
Aleksander Alekseev
?
MongoFr : MongoDB as a log Collector
MongoFr : MongoDB as a log CollectorMongoFr : MongoDB as a log Collector
MongoFr : MongoDB as a log Collector
Pierre Baillet
?
strangeloop 2012 apache cassandra anti patterns
strangeloop 2012 apache cassandra anti patternsstrangeloop 2012 apache cassandra anti patterns
strangeloop 2012 apache cassandra anti patterns
Matthew Dennis
?
Update on Crimson - the Seastarized Ceph - Seastar Summit
Update on Crimson  - the Seastarized Ceph - Seastar SummitUpdate on Crimson  - the Seastarized Ceph - Seastar Summit
Update on Crimson - the Seastarized Ceph - Seastar Summit
ScyllaDB
?
Invitation to mongo db @ Rakuten TechTalk
Invitation to mongo db @ Rakuten TechTalkInvitation to mongo db @ Rakuten TechTalk
Invitation to mongo db @ Rakuten TechTalk
Ryuji Tamagawa
?
Avoiding Data Hotspots at Scale
Avoiding Data Hotspots at ScaleAvoiding Data Hotspots at Scale
Avoiding Data Hotspots at Scale
ScyllaDB
?
Embulk and Machine Learning infrastructure
Embulk and Machine Learning infrastructureEmbulk and Machine Learning infrastructure
Embulk and Machine Learning infrastructure
Hiroshi Toyama
?
Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...
Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...
Cassandra Summit 2014: Reading Cassandra SSTables Directly for Offline Data A...
DataStax Academy
?

Similar to Node.js streams talk (20)

A first look into the Project Loom in Java
A first look into the Project Loom in JavaA first look into the Project Loom in Java
A first look into the Project Loom in Java
Lukas Steinbrecher
?
Play Framework
Play FrameworkPlay Framework
Play Framework
Eduard Tudenhoefner
?
Nodejs
NodejsNodejs
Nodejs
Vinod Kumar Marupu
?
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
Tech in Asia ID
?
NodeJS
NodeJSNodeJS
NodeJS
LinkMe Srl
?
Nick Taylor - A Full Stack Web Framework for Deno
Nick Taylor - A Full Stack Web Framework for DenoNick Taylor - A Full Stack Web Framework for Deno
Nick Taylor - A Full Stack Web Framework for Deno
All Things Open
?
Nodejs web service for starters
Nodejs web service for startersNodejs web service for starters
Nodejs web service for starters
Bruce Li
?
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
Iskren Chernev
?
Road to sbt 1.0 paved with server
Road to sbt 1.0   paved with serverRoad to sbt 1.0   paved with server
Road to sbt 1.0 paved with server
Eugene Yokota
?
An overview of node.js
An overview of node.jsAn overview of node.js
An overview of node.js
valuebound
?
Riereta Node.js session 3 (with notes)
Riereta Node.js session 3 (with notes)Riereta Node.js session 3 (with notes)
Riereta Node.js session 3 (with notes)
Tekno Paul
?
gen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixirgen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixir
Tomá? Koutsk?
?
There is REST and then there is "REST"
There is REST and then there is "REST"There is REST and then there is "REST"
There is REST and then there is "REST"
Radovan Semancik
?
Node ppt
Node pptNode ppt
Node ppt
Tamil Selvan R S
?
Javascript Update May 2013
Javascript Update May 2013Javascript Update May 2013
Javascript Update May 2013
RameshNair6
?
Microservices in Scala: Play Framework
Microservices in Scala: Play FrameworkMicroservices in Scala: Play Framework
Microservices in Scala: Play Framework
?ukasz Sowa
?
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Eugene Yokota
?
Build reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQ
Robin Xiao
?
On component interface
On component interfaceOn component interface
On component interface
Laurence Chen
?
Building a Database for the End of the World
Building a Database for the End of the WorldBuilding a Database for the End of the World
Building a Database for the End of the World
jhugg
?
A first look into the Project Loom in Java
A first look into the Project Loom in JavaA first look into the Project Loom in Java
A first look into the Project Loom in Java
Lukas Steinbrecher
?
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
Tech in Asia ID
?
Nick Taylor - A Full Stack Web Framework for Deno
Nick Taylor - A Full Stack Web Framework for DenoNick Taylor - A Full Stack Web Framework for Deno
Nick Taylor - A Full Stack Web Framework for Deno
All Things Open
?
Nodejs web service for starters
Nodejs web service for startersNodejs web service for starters
Nodejs web service for starters
Bruce Li
?
The art of concurrent programming
The art of concurrent programmingThe art of concurrent programming
The art of concurrent programming
Iskren Chernev
?
Road to sbt 1.0 paved with server
Road to sbt 1.0   paved with serverRoad to sbt 1.0   paved with server
Road to sbt 1.0 paved with server
Eugene Yokota
?
An overview of node.js
An overview of node.jsAn overview of node.js
An overview of node.js
valuebound
?
Riereta Node.js session 3 (with notes)
Riereta Node.js session 3 (with notes)Riereta Node.js session 3 (with notes)
Riereta Node.js session 3 (with notes)
Tekno Paul
?
gen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixirgen_udp and gen_tcp in Elixir
gen_udp and gen_tcp in Elixir
Tomá? Koutsk?
?
There is REST and then there is "REST"
There is REST and then there is "REST"There is REST and then there is "REST"
There is REST and then there is "REST"
Radovan Semancik
?
Javascript Update May 2013
Javascript Update May 2013Javascript Update May 2013
Javascript Update May 2013
RameshNair6
?
Microservices in Scala: Play Framework
Microservices in Scala: Play FrameworkMicroservices in Scala: Play Framework
Microservices in Scala: Play Framework
?ukasz Sowa
?
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)Road to sbt 1.0: Paved with server (2015 Amsterdam)
Road to sbt 1.0: Paved with server (2015 Amsterdam)
Eugene Yokota
?
Build reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQ
Robin Xiao
?
Building a Database for the End of the World
Building a Database for the End of the WorldBuilding a Database for the End of the World
Building a Database for the End of the World
jhugg
?

Recently uploaded (20)

Sccccccccccccccccccccccannig Network.pptx
Sccccccccccccccccccccccannig Network.pptxSccccccccccccccccccccccannig Network.pptx
Sccccccccccccccccccccccannig Network.pptx
rsi3pfe
?
Lecture 3 - Types of Distributed Systems.ppt
Lecture 3 - Types of Distributed Systems.pptLecture 3 - Types of Distributed Systems.ppt
Lecture 3 - Types of Distributed Systems.ppt
KostadinKostadin
?
加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭
加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭
加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭
taqyed
?
hisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docx
hisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docxhisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docx
hisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docx
hisseyorumu.com
?
Reading Challenge 1_2nd_Answer Key ?áp án.pdf
Reading Challenge 1_2nd_Answer Key ?áp án.pdfReading Challenge 1_2nd_Answer Key ?áp án.pdf
Reading Challenge 1_2nd_Answer Key ?áp án.pdf
MaiLin27
?
types of interview 2.pptxvvvvvvvvvvvvvvvv
types of interview 2.pptxvvvvvvvvvvvvvvvvtypes of interview 2.pptxvvvvvvvvvvvvvvvv
types of interview 2.pptxvvvvvvvvvvvvvvvv
khizeraftab1018
?
Verbal_Memory_Function_in_Intellectually (1).pdf
Verbal_Memory_Function_in_Intellectually (1).pdfVerbal_Memory_Function_in_Intellectually (1).pdf
Verbal_Memory_Function_in_Intellectually (1).pdf
arnikbotikara
?
032ssssssssssssssssssssssssssssssssssss024-PBD.pptx
032ssssssssssssssssssssssssssssssssssss024-PBD.pptx032ssssssssssssssssssssssssssssssssssss024-PBD.pptx
032ssssssssssssssssssssssssssssssssssss024-PBD.pptx
Anonymoushbl0ek0qdZ
?
Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...
Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...
Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...
JaneCalzada
?
MIL Lesson 9 - Text Information and Media.pptx
MIL Lesson 9 - Text Information and Media.pptxMIL Lesson 9 - Text Information and Media.pptx
MIL Lesson 9 - Text Information and Media.pptx
OmarPogi1
?
Kalin Karakehayov - The mindset and habits for building an SEO Empire
Kalin Karakehayov - The mindset and habits for building an SEO EmpireKalin Karakehayov - The mindset and habits for building an SEO Empire
Kalin Karakehayov - The mindset and habits for building an SEO Empire
Kalin Karakehayov
?
How to Create a Digital Marketing Strategy - Leadcrew
How to Create a Digital Marketing Strategy - LeadcrewHow to Create a Digital Marketing Strategy - Leadcrew
How to Create a Digital Marketing Strategy - Leadcrew
Leadcrew - Digital Marketing Agency in Karachi
?
Combating Phishing Emails with SPF, DKIM, DMARC
Combating Phishing Emails with SPF, DKIM, DMARCCombating Phishing Emails with SPF, DKIM, DMARC
Combating Phishing Emails with SPF, DKIM, DMARC
Ade Ismail Isnan
?
Lecture 2 - Definition and Goals of a Distributed System.ppt
Lecture 2 - Definition and Goals of a Distributed System.pptLecture 2 - Definition and Goals of a Distributed System.ppt
Lecture 2 - Definition and Goals of a Distributed System.ppt
KostadinKostadin
?
Sample presentation to test embedding into an app
Sample presentation to test embedding into an appSample presentation to test embedding into an app
Sample presentation to test embedding into an app
firefields
?
A comprehensive guide to Starlink technology and its availability in Florida.pdf
A comprehensive guide to Starlink technology and its availability in Florida.pdfA comprehensive guide to Starlink technology and its availability in Florida.pdf
A comprehensive guide to Starlink technology and its availability in Florida.pdf
Internet Bundle Now
?
HiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptx
HiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptxHiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptx
HiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptx
pbhathiwala
?
Building High-Performing Virtual Teams.pdf
Building High-Performing Virtual Teams.pdfBuilding High-Performing Virtual Teams.pdf
Building High-Performing Virtual Teams.pdf
Will Haire
?
william Shakespeare.presentation on pptx
william Shakespeare.presentation on pptxwilliam Shakespeare.presentation on pptx
william Shakespeare.presentation on pptx
ahmedovalaziza28
?
Long Term Evolution (LTE): High-Speed Wireless Communication Explained
Long Term Evolution (LTE): High-Speed Wireless Communication ExplainedLong Term Evolution (LTE): High-Speed Wireless Communication Explained
Long Term Evolution (LTE): High-Speed Wireless Communication Explained
Marcel David
?
Sccccccccccccccccccccccannig Network.pptx
Sccccccccccccccccccccccannig Network.pptxSccccccccccccccccccccccannig Network.pptx
Sccccccccccccccccccccccannig Network.pptx
rsi3pfe
?
Lecture 3 - Types of Distributed Systems.ppt
Lecture 3 - Types of Distributed Systems.pptLecture 3 - Types of Distributed Systems.ppt
Lecture 3 - Types of Distributed Systems.ppt
KostadinKostadin
?
加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭
加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭
加拿大成绩单购买原版(罢惭鲍毕业证书)多伦多都会大学毕业证文凭
taqyed
?
hisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docx
hisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docxhisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docx
hisse yorumu: BofA haberleri, Hedef Fiyat, Halka arz.docx
hisseyorumu.com
?
Reading Challenge 1_2nd_Answer Key ?áp án.pdf
Reading Challenge 1_2nd_Answer Key ?áp án.pdfReading Challenge 1_2nd_Answer Key ?áp án.pdf
Reading Challenge 1_2nd_Answer Key ?áp án.pdf
MaiLin27
?
types of interview 2.pptxvvvvvvvvvvvvvvvv
types of interview 2.pptxvvvvvvvvvvvvvvvvtypes of interview 2.pptxvvvvvvvvvvvvvvvv
types of interview 2.pptxvvvvvvvvvvvvvvvv
khizeraftab1018
?
Verbal_Memory_Function_in_Intellectually (1).pdf
Verbal_Memory_Function_in_Intellectually (1).pdfVerbal_Memory_Function_in_Intellectually (1).pdf
Verbal_Memory_Function_in_Intellectually (1).pdf
arnikbotikara
?
032ssssssssssssssssssssssssssssssssssss024-PBD.pptx
032ssssssssssssssssssssssssssssssssssss024-PBD.pptx032ssssssssssssssssssssssssssssssssssss024-PBD.pptx
032ssssssssssssssssssssssssssssssssssss024-PBD.pptx
Anonymoushbl0ek0qdZ
?
Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...
Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...
Lesson-1-Multimedia (1) Explore the principle of interactivity and rich conte...
JaneCalzada
?
MIL Lesson 9 - Text Information and Media.pptx
MIL Lesson 9 - Text Information and Media.pptxMIL Lesson 9 - Text Information and Media.pptx
MIL Lesson 9 - Text Information and Media.pptx
OmarPogi1
?
Kalin Karakehayov - The mindset and habits for building an SEO Empire
Kalin Karakehayov - The mindset and habits for building an SEO EmpireKalin Karakehayov - The mindset and habits for building an SEO Empire
Kalin Karakehayov - The mindset and habits for building an SEO Empire
Kalin Karakehayov
?
Combating Phishing Emails with SPF, DKIM, DMARC
Combating Phishing Emails with SPF, DKIM, DMARCCombating Phishing Emails with SPF, DKIM, DMARC
Combating Phishing Emails with SPF, DKIM, DMARC
Ade Ismail Isnan
?
Lecture 2 - Definition and Goals of a Distributed System.ppt
Lecture 2 - Definition and Goals of a Distributed System.pptLecture 2 - Definition and Goals of a Distributed System.ppt
Lecture 2 - Definition and Goals of a Distributed System.ppt
KostadinKostadin
?
Sample presentation to test embedding into an app
Sample presentation to test embedding into an appSample presentation to test embedding into an app
Sample presentation to test embedding into an app
firefields
?
A comprehensive guide to Starlink technology and its availability in Florida.pdf
A comprehensive guide to Starlink technology and its availability in Florida.pdfA comprehensive guide to Starlink technology and its availability in Florida.pdf
A comprehensive guide to Starlink technology and its availability in Florida.pdf
Internet Bundle Now
?
HiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptx
HiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptxHiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptx
HiSTEAM 2nd onwwwwwwwwwwwwwwwwwwwww.pptx
pbhathiwala
?
Building High-Performing Virtual Teams.pdf
Building High-Performing Virtual Teams.pdfBuilding High-Performing Virtual Teams.pdf
Building High-Performing Virtual Teams.pdf
Will Haire
?
william Shakespeare.presentation on pptx
william Shakespeare.presentation on pptxwilliam Shakespeare.presentation on pptx
william Shakespeare.presentation on pptx
ahmedovalaziza28
?
Long Term Evolution (LTE): High-Speed Wireless Communication Explained
Long Term Evolution (LTE): High-Speed Wireless Communication ExplainedLong Term Evolution (LTE): High-Speed Wireless Communication Explained
Long Term Evolution (LTE): High-Speed Wireless Communication Explained
Marcel David
?

Node.js streams talk

  • 1. ROW ROW ROW YOUR BOAT by @zladuric
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. The past Let's hope it dies!
  • 6. 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?
  • 7. 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
  • 8. The present Node.js “streams2”: Stability: 2 – Unstable ● Node.js is unstable ;) ● No breaking changes expected for Node.js 0.12
  • 9. 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();
  • 10. How to be streamin' in Node $(#button).on('click', handler); anyone? Dumb example
  • 11. stream.Readable ● Flowing and non-flowing? ● Common events and methods ● Examples: fs.createReadStream, http.createServer listeners, tcp and net sockets.
  • 12. 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.”
  • 13. 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();
  • 14. Borin Simple example ● this.push(); → stream.read(); ● objectMode is handy
  • 15. Writable stream ● To implement a writable stream, implement _write method, drain
  • 16. Transform streams Splits input into lines ● _transform() - what to do on input data ● _flush – what to do when input ends
  • 17. And now pipes! var stream = getSomeReadStream() .pipe(toConverterStream()) .pipe(toEncoderStream()) .pipe(toTransportStream()) .pipe(toTheConsumerStream());
  • 18. Piping examples Read file line by line Top secret Steal Google logo
  • 19. Do you use it?
  • 20. The client ● WebRTC, WebSockets, Workers – they all stream data ● XMLHttpRequest ● User Media (MediaStream) API, Blob/FileSystem API, Audio, Geolocation etc.
  • 21. 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 = /slideshow/nodejs-streams-talk/39693897/window.URL.createObjectURL(localMediaStream); }, console.log); </script>
  • 22. 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
  • 23. The future ● Node.js is relatively stable ● Whatwg is trying to standardize stream APIs from: Domenic Denicola
  • 25. Question time 1. Was my english ok? 2. How boring was I, from 1 – 10? Your turn. Zlatko ?uri?