An Introduction to Node
From David Bella hosted by NYC Open Data
January 30, 2014
A Little About Me
• B.S. Computer Science from UCONN
• 2 Years of ETL in Finance (Perl)
• Attended The Flatiron School Ruby 003 in September
• Currently Software Engineer at XO Group (Rails)
• Join us! XO Hackathon February 8th and 9th - bit.ly/xohack
What is Node?
Some confusing terminology
• Web Server?
What is Node?
False comparisons to other technology
• Rails - A Web Framework
• nginx - A Web Server
• Node - A Non-Blocking, Event-Driven, Networking Platform
What is Node?
What is Node Made Of?
• Abstraction Layer
• Core Library
These 3-4 items make up what we know as "Node"
One might say JS is to V8 as Ruby is to MRI, maybe
Node and V8
Node uses the V8 Engine
Node's abstraction layer is an implementation of the Reactor Pattern
Node's core library makes use of the reactor pattern to handle requests
Ruby's EventMachine is an implementation of the reactor pattern
The Reactor Pattern
The main event loop is non-blocking and waits for work requests
Requests are dispatched out to the reactor by the event loop
The reactor is a single threaded worker that takes work requests,
processes them one at a time until fully completed, then starts the next
There is nothing magical or Node specific about callbacks
It is simply a function passed into another function that gets executed at
some point - usually when that second function is finished or has
something to report to the caller, like an error
Callbacks and Events
This is an example of what we mean when we say Node is Event Driven
Callbacks/Events are important in Node so that we know when Node is
done running whatever work we gave it
Node can alert us of an event by using a callback
Tying It All Together...
• Node implements an event-driven Reactor pattern
Wonderful! Node ties together two beautiful
concepts - evented programming and an
event based pattern. so much. wow.
So - Why's It Good?
Highly concurrent - the event based architecture helps handle requests
without having to create new threads and switch between them
Real time, two way connections - Create a richer web experience for the
user (we'll get to this much later)
Open Web Stack - HTML, CSS, JS are a dream team, now we have JS on
the backend, further reducing complexity
And... Why's It Bad?
It's not a silver bullet for all issues
Not great at single threaded CPU intensive calculations
to get used to
Short Note about NPM
NPM is the Node Package Maid Manager
Allows you to pull in extra packages for Node that you can then require
• npm install -g <package_name> # Installs globally
• npm install <package_name> # Installs locally
What We Are Making
A simple REST API powered by Express, backed by MongoDB
A front end with Backbone.js for rendering the collection
Simple publish subscribe functionality with Faye to provide live updates
Questions and Thanks!
A huge thank you to:
• Vivian and NYC Open Data
• Ryan Dahl and Joyent for making and supporting Node
• Vadim Makeev for the Shower presentation software
• Everyone here!
• XO Group for employing me!