Slide 1

Slide 1 text

Streams in Node.js the old, the new, and the why Charles Julian Knight @ c h a r l e s j u l i a n k

Slide 2

Slide 2 text

Intro Why you care: Elegant, powerful code Easy to follow Many modules use it

Slide 3

Slide 3 text

Concept: pipes "We should have some ways of coupling programs like garden hose -- screw in another segment when it becomes when it becomes necessary to massage data in another way. This is the way of IO also." -- Douglas McIlroy, 1964

Slide 4

Slide 4 text

Some examples

Slide 5

Slide 5 text

Node Streams stream.Readable stream.Writable stream.Duplex stream.Transform

Slide 6

Slide 6 text

Readable Examples f s . c r e a t e R e a d S t r e a m h t t p . I n c o m i n g M e s s a g e Implement u t i l . i n h e r i t s ( m y S t r e a m , s t r e a m . R e a d a b l e ) ; m y S t r e a m . p r o t o t y p e . r e a d = f u n c t i o n ( ) . . .

Slide 7

Slide 7 text

Writable Examples f s . c r e a t e W r i t e S t r e a m h t t p . S e r v e r R e s p o n s e Implement u t i l . i n h e r i t s ( m y S t r e a m , s t r e a m . W r i t a b l e ) ; m y S t r e a m . p r o t o t y p e . w r i t e = f u n c t i o n ( ) . . .

Slide 8

Slide 8 text

Duplex Both worlds! Example n e t . S o c k e t Implement u t i l . i n h e r i t s ( m y S t r e a m , s t r e a m . D u p l e x ) ; m y S t r e a m . p r o t o t y p e . r e a d = f u n c t i o n ( ) . . . m y S t r e a m . p r o t o t y p e . w r i t e = f u n c t i o n ( ) . . .

Slide 9

Slide 9 text

Old streams Readable o n ( ' d a t a ' ) o n ( ' e n d ' ) r e a d ( ) p a u s e ( ) r e s u m e ( ) u n s h i f t ( )

Slide 10

Slide 10 text

Old streams Writable o n ( ' d r a i n ' ) o n ( ' f i n i s h ' ) w r i t e ( ) e n d ( )

Slide 11

Slide 11 text

Let's try it!

Slide 12

Slide 12 text

Problems data comes immediately p a u s e ( ) doesn't pause handoff is difficult

Slide 13

Slide 13 text

Streams2 Implemented in v 0 . 1 0 new event: 'readable' p a u s e ( ) , r e s u m e ( ) , o n ( ' d a t a ' ) or o n ( ' e n d ' ) revert to old functionality Allows for arbitrary Objects as well as Buffers and Strings

Slide 14

Slide 14 text

Refactor!

Slide 15

Slide 15 text

And now for pipes MP3 Player s p e a k e r - takes in a stream of PCM audio data and plays it through the speakers. l a m e - Encodes/decodes MP3's to/from raw PCM formats.

Slide 16

Slide 16 text

contact [email protected] thanks @ s u b s t a c k @ d o m i n i c t a r r @ t o o t a l l n a t e MarkdownPresenter slides & notes speakerdeck.com/rabidaudio/node-streams github.com/rabidaudio/stream-talk