NodeJS and JavaScript- everywhere

bootlessbwakInternet και Εφαρμογές Web

12 Νοε 2013 (πριν από 3 χρόνια και 10 μήνες)

85 εμφανίσεις

NodeJS and JavaScript-
everywhere
Matthew Eernisse
Devoxx Conference, April 2011
Thursday, April 19, 12
Who am I?
Matthew Eernisse
Work at Yammer
mde on Twitter
Thursday, April 19, 12
What is “JavaScript-
everywhere”?
Thursday, April 19, 12
A list of stuff

Ruby

JavaScript

Scala

Java

C

Rails

NodeJS

PostgreSQL

Memcache

Riak

Redis

BDB

Vertica

R
Thursday, April 19, 12
JavaScript at Yammer

Browsers YamJS, a.k.a.,“YamJuice”

Adobe AIR Desktop

V8 in Rails via TheRubyRacer

NodeJS
Thursday, April 19, 12
Server JS
Thursday, April 19, 12

Netscape Enterprise Server (OG SSJS)

Microsoft IIS

Helma (now RingoJS)

Whitebeam

Zimki

Jaxer

Perservere

Nitro

Google App Engine

CouchDB

NodeJS
SSJS
Thursday, April 19, 12
Why NodeJS?

Death of Netscape

Open source

AJAX

Yegge’s NBL, 2007-02-10

Competition

V8
Thursday, April 19, 12
NodeJS:
“Evented I/O for V8 JavaScript”
http://nodejs.org
/
Thursday, April 19, 12
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/'
);
Hello, NodeJS
Thursday, April 19, 12
What can you do with
Node?
Thursday, April 19, 12
EventEmitter
Readable/WritableStream
Callbacks
Thursday, April 19, 12
NodeJS at Yammer
Thursday, April 19, 12
Jake build tool
https://github.com/mde/jake

Tasks, prerequisites

File tasks, directory tasks, PackageTask, NPMPublishTask

Namespaces

jake.exec, fileUtils

Just executable JavaScript

Async task execution (shell commands)
Thursday, April 19, 12
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});
});
Thursday, April 19, 12
Geddy Web framework:
http://geddyjs.org/
Thursday, April 19, 12
$ geddy app [my_app]
$ geddy resource [my_resource]
Thursday, April 19, 12
NodeJS at Yammer

Upload service for files and images

Realtime, collaborative document-
editing feature
Thursday, April 19, 12
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
Thursday, April 19, 12
Realtime, collaborative
documents feature: Pages

In beta Oct. 21, 2011

EtherPad Lite (
https://github.com/Pita/
etherpad-lite
): NodeJS, Socket.io, PostgreSQL
Thursday, April 19, 12
Coding JS for Node
Thursday, April 19, 12
Flexibility
Thursday, April 19, 12

Even shelling out is async?

“Inside-out” execution

Evented and callback-based control-flow

Easy for the inexperienced to create
memleak
Async horror
Thursday, April 19, 12
var fetchAndUpdate = function (params) {
var items = db.fetch(someQuery);
for (var i = 0, ii = items.length; i < ii; i++) {
item.update(params);
}
return true;
};
Sync fetch-and-update
Thursday, April 19, 12
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);
}
});
}
});
};
Async fetch-and-update
Thursday, April 19, 12
jQuery.ajax({
url: '/foo/bar.json'
, success: function () {
alert('yay!');
}
});
jQuery('#foo').bind('click', function (e) {
// Do some stuff
});
Is this familiar?
Thursday, April 19, 12
Async patterns and libs

Async queue

Promise/deferred/future/eventual
Thursday, April 19, 12
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!');
});
Promise
Thursday, April 19, 12
Don’t overdo it.
Thursday, April 19, 12
NodeJS in production
Thursday, April 19, 12

NPM is awesome (shrinkwrap deps)

Third-party modules still may change
rapidly

Check in deps

Maintain forks, push back patches where
appropriate
App dependencies
Thursday, April 19, 12
Assume you’re fucked
Callbacks in global scope
have no stack
Thursday, April 19, 12

uncaughtException handler

Preemptible timeout-errors

Timeout registry
Thursday, April 19, 12
Timeout registry

Entries in an object with keys

Per-item timeout

Configurable polling-interval

Define a timeout-handler
Thursday, April 19, 12
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
});
Timeout registry
Thursday, April 19, 12

Measure everything

Log everything

https://github.com/mikejihbe/metrics
Visibility, metrics
Thursday, April 19, 12

Communicative, consultative dev

Ask what is expected

Be transparent

Play nicely with others
Ops
Thursday, April 19, 12
Try it; you’ll like it.
Thursday, April 19, 12
Matthew Eernisse
http://twitter.com/mde
Yammer Developer Center
http://developer.yammer.com/
Yammer Engineering Blog
http://eng.yammer.com
/
Thursday, April 19, 12