node everywhere


The Death Star: 

An Adventure in Loose Coupling



Joshua Wulf

jwulf@redhat.com

github/jwulf

@sitapati

The Death Star


Sky Net


sky net

"Sentient Documentation"

SKY NET


In Sky Net, Books Read You  

  • Sensing 
  • Self-modifying

SKY NET


What could possibly go wrong?


User-Friendly



Installation

meteor.com


curl https://install.meteor.com | sh

Death Star


curl https://tinyurl.com/get-deathstar | sh

dependencies

DEPENDENCIES


1.7 GB

DEPENDENCIES


3.2 GB

dependencies


GET DEATH Star

GET death star


Get death star


Get death star


Get death star


PUBlican


BREW


Death star



Loosely coupled







DEMONSTration





1. EVENTS


2. STREAMS


1. EVENTS



EVENTS


  • User initiates action via REST call
  • Server commences operation
  • Server updates Book metadata
  • Server notifies clients of event via websocket
  • Clients update status in embedded control panel

EVENTS


  • User initiates action via REST call
  • Server commences operation
  • Server updates Book metadata
  • Server notifies client of event via websocket

EVENT NOTIFICATION


function RESTRequestBuild (bookid) { 
   
   Builder.build(bookid);
   Books[bookid].building = true;
   notifier.notify(bookid, 'building'); 

}

"EVENTED OBJECT"


http://tinyurl.com/node-evented-cache

  • Extend EventEmitter Class
  • Key:Value pair object
  • Setters that emit events

"EVENTED OBJECT"


    
  Book = new EventedBook ();
  Book.set('myprop', true);
  Book.myprop = false;

  Book.on('change', function (event) {
     console.log(event);
  });

  Output:
  {myprop, true}
  {myprop, false}

EVENTED OBJECT -> WEB SOCKET

var io = require('socket.io').listen(server);
io.on('connection', sockethandler);
function sockethandler(client) {
   client.on('booksubscribe', function (req) {
      var bookid = req.bookid;
      Books[bookid].on('change', 
        function (event) {
          client.emit('change', event);
      }
   });
}
     

EVENT NOTIFICATION 2.0


function RESTRequestBuild (bookid) { 
   Builder.build(bookid);
   
   // now emits event
   Books[bookid].building = true;
  
   // unnecessary
   // notifier.notify(bookid, 'building');   
}



2. STREAMS

SERVER PROCESS INFORMATION


  • Server-side processes produce output
  • Users need to see server-side process:
    Status
    Error information

STREAMS


var Streams = {};

function build (bookid) { 
   Streams[bookid] = new Stream();

   var job = exec.spawn('csprocessor', ['build', bookid]);   

   job.stdout.pipe(Streams[bookid]);
}
Note: stream writable and .write function not shown

STREAM -> WeB SOCKET

function sockethandler () {
   client.on('getStream', function (req) {  
      var bookid = req.bookid;
      builder.Streams[bookid].on('data',
          function (data) {
             client.emit('stream', data);
         });
   }
}

SUMMARY


Loose coupling through:

  • Evented objects + web sockets
  • Streams + web sockets

I, for one, welcome our new Robotic Overlords.