Streams are awesome.



@sh1mmer
Tom Hughes-Croucher
       @sh1mmer
Scalable Server-Side Code with JavaScript




               Node                        Up and Running




                                          Tom Hughes-Croucher




    http://ofps.oreilly.com/titles/9781449398583/
http://shop.oreilly.com/product/0636920015956.do
Client → ServerServer → DB




                  Computation Computation
@sh1mmer
Non event-driven
              frontend


@sh1mmer
@sh1mmer
Server




@sh1mmer
Request




@sh1mmer
@sh1mmer
Event-driven frontend



@sh1mmer
Place-
           holder




@sh1mmer
@sh1mmer
Shared
  Work
Resources




@sh1mmer
Memory operations



@sh1mmer
var x = "I am a string"

           ~1ns Running 1 instruction
           2ns Data from l1 cpu cache
           5ns Data from l2 cpu cache
           80ns Data from ram




@sh1mmer
@sh1mmer
Mobile data latency



@sh1mmer
@sh1mmer
100 Blue Whales

                      Cat
@sh1mmer
Node.js Streams



@sh1mmer
Streams
•   Readable
    •   Pause
    •   Resume
    •   data event
•   Writable
    •   Write
•   Pipe
Event Loop Fail

          node.cc

     add-ons          v8

              libuv

      libev           iocp
nextTick
                       Event Loop Fail
nextTick
                      Run stuff
                      TCP
                      Conn             node.cc
                            FS
                           Read

                                  add-ons          v8
                       TCP
                       Conn
                            v8
                           func            libuv
   v8                 TCP
  func                Conn
                                   libev           iocp
       v8            FS
      func          Read
              FS
             Read
stream.pause()




@sh1mmer
nextTick
                       Event Loop Fail
nextTick
                       stream.pause()
                      TCP
                      Conn              node.cc
                            FS
                           Read

                                  add-ons          v8
                       TCP
                       Conn
                            v8
                           func            libuv
   v8                 TCP
  func                Conn
                                   libev           iocp
       v8            FS
      func          Read
              FS
             Read
New Streams
•   Writable
    •   Same (hooray!)
•   Readable
    •   Configure buffer waterline
    •   readable event
    •   read(len) -> returns data
•   Pipe
    •   Doesn't mix with direct access
Node.js Domains



@sh1mmer
var d = domain.create();

  function readSomeFile(filename, cb) {
    fs.readFile(filename, 'utf8', d.bind(function(er, data) {
      // if this throws, it will also be passed to the domain
      return cb(er, data ? JSON.parse(data) : null);
    }));
  }

  d.on('error', function(er) {
    // an error occurred somewhere.
    // if we throw it now, it will crash the program
    // with the normal line number and stack message.
  });



@sh1mmer
Questions!



@sh1mmer

Streams are Awesome - (Node.js) TimesOpen Sep 2012