Share
Embed
Node.js: How and Why?
A Penn Apps Fall 2013 Tech Talk
September 4, 2013
Ankur Oberoi
September 04, 2013
Transcript
16 slides
-
The OpenTok API adds live streaming video to your web
or mobile app. Ankur Oberoi Developer Evangelist, TokBox @aoberoi | [email protected] -
What is Node? • V8 JavaScript Environment • Event based
I/O • Some Uses: - Network Programs - Web Servers - Web Sockets! - Command line utilities - Robots! -
Why Node? • Single-threaded - Stance: threads add complexity in
the wrong place - data races - mutual exclusion / data locking - deadlock - ... too much overhead -
Why Node? • Because its JavaScript - V8 is super
fast - Browser Wars work in our favor for speed - Reuse front-end knowledge, less context-switching - Functional (first class function) - Object-Oriented - Most common programming language in the world -
Why Node? • Async all the things! (non-blocking standard library)
- Direct consequence of limiting to a single thread - No legacy compatibility to worry about - All code must purposely be designed around async, less chances of horrible dependencies • Concurrency complexity in the right place - Single-threaded means only utilizing a single core per process - So... start more processes! - You will naturally divide your programs into parts with meaningful interfaces. This puts the complexity in the right place. -
Why Node? • Platform Support - OS X - Windows
- Linux - Smart OS -
Why Node, over others? • Can we take these principles
and apply them elsewhere? • For some environments, its too late: - Rails is already built, it would require changing everything • Even if it can be done, its already a mixed bag: - Twisted Python is an event-driven networking framework - The entire ecosystem of reusable code would need to be rebuilt to work with it • Real Time Web Apps! - All this stuff is pretty necessary when you start thinking about real-time -
CommonJS modules // use require to import installed/system modules var
fs = require('fs'); // a param that looks like a file path to directly import source var foo = require('./foo'); // expose functions by attaching them to exports global object exports.addOne = function(x) { return x + 1; } // OR expose whole objects by assigning to module.exports function Person(name) { this.name = name; } module.exports = Person; -
Callback Interfaces // sometimes the results of a function call
cannot be returned immediately, // (like when it does some I/O), get results with a callback // you can implement a callback interface for functions you expose var dns = require('dns'); dns.lookup('google.com', function(err, ipAddress) { // NODE CONVENTION: the first parameter given to a callback is the error // check if it is truthy before continuing if (err) return handleError(err); console.log('google.com resolved to %s', ipAddress); }); // you can write functions that return results in a callback too function addOneLater(x, cb) { setTimeout(function() { cb(null, x + 1); }, 200); } -
Event Emitters // Event emitters are another common pattern for
recieving interesting // information from an object over time var net = require('net'); var server = net.createServer(); // when you use the .on() method, its an event emitter server.on('connection', function (stream) { // this will happen over and over every time a connection comes in console.log('someone connected!'); }); server.listen(1337); -
Streams // A request is a readable stream var http
= require('http'); http.createServer(function(req, res) { req.on('data', function(chunk) { console.log('got %d bytes of data', chunk.length); }); req.on('end', function() { console.log('end of request'); }); }); // You can get a writable stream to the filesystem var fs = require('fs'); var ws = fs.createWriteStream('tmp.txt'); ws.write('hello world'); ws.end(); -
Where do I host it? • Heroku (no WebSockets) •
Nodejitsu • AWS-Box