NodeJS and JavaScripteverywhere Matthew Eernisse YOW Conference: December 2011
Who am I? Matthew Eernisse Work at Yammer @mde on Twitter
What is JavaScript- everywhere?
A list of stuff Ruby JavaScript Scala Java C# Rails NodeJS PostgreSQL Memcache Riak Redis BDB Vertica R
JavaScript at Yammer Browsers YamJS, a.k.a., YamJuice Adobe AIR Desktop V8 in Rails via TheRubyRacer NodeJS
Server JS
SSJS Netscape Enterprise Server (OG SSJS) Microsoft IIS Helma (now RingoJS) Whitebeam Zimki Jaxer Perservere Nitro Google App Engine CouchDB NodeJS
Why NodeJS? Death of Netscape Open source Yegge s NBL, 2007-02-10 Competition V8
NodeJS: Evented I/O for V8 JavaScript http://nodejs.org/
Hello, NodeJS var http = require('http'); http.createserver(function (req, res) { res.writehead(200, {'Content-Type': 'text/plain'}); res.end('hello World\n'); }).listen(1337, "127.0.0.1"); console.log('server running at ' + ' http://127.0.0.1:1337/');
What can you do with Node?
EventEmitter Readable/WritableStream Callbacks
Jake build tool https://github.com/mde/jake Tasks, prerequisites File tasks, directory tasks Namespaces PackageTasks Just executable JavaScript Async task execution (shell commands)
namespace('foo', function () { desc('this a sychronous task'); task('bar', function () { console.log('howdy'); }); }); desc('this an asychronous task'); task('baz', function () { require('child_process').exec('ls -l', function (err, stdout, stderr) { console.log(stdout); complete(); }); }, {async: true});
Geddy Web framework: https://github.com/mde/geddy
NodeJS at Yammer Upload service for files and images Realtime, collaborative documentediting feature Another, early-alpha service for mobile Web
Remote upload service Minimal v1 in prod, Nov. 2010 Onboard thumbnailing, remote services for video and document post-processing Redis, CORS XHR-push or JSONP for upload-progress reporting
Realtime, collaborative documents feature In beta Oct. 21, 2011 EtherPad Lite (https://github.com/pita/ etherpad-lite): NodeJS, Socket.io, PostgreSQL
Coding JS for Node
Flexibility
Async horror Even shelling out is async? Inside-out execution Evented and callback-based control-flow
Sync fetch-and-update var fetchandupdate = function (params) { var items = db.fetch(somequery); for (var i = 0, ii = items.length; i < ii; i++) { item.update(params); } return true; };
Async fetch-and-update var fetchandupdate = function (params, callback) { db.fetch(somequery, function (items) { var count = 0; for (var i = 0, ii = items.length; i < ii; i++) { item.update(params, function () { count++; if (count == ii) { callback(true); } }); } }); };
Is this familiar? jquery.ajax({ url: '/foo/bar.json', success: function () { alert('yay!'); } }); jquery('#foo').bind('click', function (e) { // Do some stuff });
Async patterns and libs Async queue Promise/deferred/future/eventual
Promise var p = new yammer.util.promise(); p.when('foo', 'bar', 'baz').then( function () { console.log('done!'); }); p.satisfy('foo'); p.satisfy('bar'); p.satisfy('baz'); p.then(function () { console.log('still done!'); });
Don t overdo it.
NodeJS in production
App dependencies NPM is awesome Third-party modules still may change rapidly Maintain forks, push back patches where appropriate
Assume you re fucked Callbacks in global scope have no stack
uncaughtexception handler Preemptible timeout-errors Timeout registry
Timeout registry Entries in an object with keys Per-item timeout Configurable polling-interval Define a timeout-handler
Timeout registry var TimeoutReg = require('timeout_registry').timeoutreg, handler = function (req, resp) { var registry = new TimeoutReg(function (key) { resp.writehead(500); resp.end('oops, something bad happened.'); }); registry('foo', 10000); performfoo(req, function (result) { if (result.ok) { registry.clear('foo'); // Do some other stuff resp.writehead(200); resp.end('hooray!'); } }); }; process.on('uncaughtexception', function (err) { // Do some kind of logging });
Visibility, metrics Measure everything Log everything https://github.com/mikejihbe/metrics
Ops Communicative, consultative dev Ask what is expected Be transparent Play nicely with others
Try it; you ll like it.
Matthew Eernisse http://twitter.com/mde Yammer Developer Center http://developer.yammer.com/