3. WTF is a Node.js?
•JavaScript on the Server
(or robots, helicopters, any other not-a-web-browser thing)
•Non-blocking I/O
•Callbacks, Events, and Streams
Monday, April 15, 13
5. conventional program
printLine('Enter your name:')
// waiting for I/O to happen!
var name = getLine();
// omg soooo slllloooowwwww
var r = db.save(name);
// taking literally forever
if (r === 'ok')
printLine('it worked!');
Monday, April 15, 13
8. Non-blocking I/O is not about
making I/O faster
•It's about making I/O not prevent other I/O
•Doing things in this way adds a slight amount of overhead.
•However, we're not monopolizing the process while we wait.
•Our server can be handling other requests while we're sitting
around doing nothing.
Monday, April 15, 13
9. Callback Drawbacks
•Stack gets unwound - Error().stack doesn't tell you much
•try/catch can't wrap throws effectively
•Boilerplate is hard to look at - `function` is a lot of characters
•Lots of indentation - "Christmas Tree Code"
Monday, April 15, 13
12. Node actually has a lot of
internal threads, but...
•Only one thread is running JavaScript
•That means that your JS will not have to worry about
some other JS getting in its way
•Simplifies parallel coding model dramatically
•Safe to share data between requests
•while(true); will prevent new requests
Monday, April 15, 13
14. Coroutines
•Cooperative Multitasking with yield and await keywords
•No preemption issues, same while(true); behavior
•Stack not unwound (try/throw/catch isn't broken)
•Not how JavaScript works (today)
•Requires modifying the semantics of the language
Monday, April 15, 13
18. no, we're not jerks.
•callback: lowest Node abstraction for doing one async thing
•EventEmitter: for things that happen continuously over time
Monday, April 15, 13
19. cb vs ee
•callback: "Show me all the blueprints"
•EventEmitter: "Any time you get blueprints, show me them"
way of the future
Monday, April 15, 13
20. EventEmitter
var e = new EventEmitter();
e.on('foo', function(arg) {
console.log('baz' + arg)
});
e.emit('foo', 'bar');
// prints 'bazbar'
Monday, April 15, 13
21. hello, EventEmitter
var s = new http.Server()
s.on('request', function(req, res){
res.end('hello, txjs!')
})
s.listen(80)
Monday, April 15, 13
22. Streams
•When managing data flows, control throughput or ENOMEM
•Streams are a special EventEmitter that takes care of this
•inputGoober.pipe(outputThingie)
•When you grok this, you grok Node.
•(see also: Marco Rogers talk)
Monday, April 15, 13
26. Node is JavaScript
•Node is not a language - JavaScript is the language
•Portability, Events, Run to completion:The JavaScript way
•Not developing a language removes a huge burden.
Let TC-39 andV8 fight those battles for us!
•Node.js endeavors to embody the spirit of JavaScript,
not to change it.
Monday, April 15, 13
28. Do not ignore errors
•Callbacks all get an error object as their first argument
•EventEmitter 'error' event is magical... MAGICALLY DEADLY!
•Domains and process.on('uncaughtException') to clean up, but
continuing in error is almost always terrible!
•Exactly how to "not ignore" errors is a question of some
debate. Liberal (never crash) vs Conservative (crash asap)
Monday, April 15, 13
32. Increase Portability
•libuv: Evented nonblocking I/O for Computers
•If you write JavaScript, it'll run on every OS Node runs on
•Safety and portability are default.
•Sometimes portability is compromised for "When in Rome"
eg path strings: on Windows, / on Unix
•If you write C++ addons, of course, all bets are off
Monday, April 15, 13
34. Slowness is a Bug
•Latency is far and away the most important metric
(Some would say, the only important metric!)
•Unnecessary slowness is a bug, a sign of incorrect behavior
•Necessary slowness must be isolated, reduced in scope
•A slow thing should not ever make other things slow
•This is what that nonblocking callback stuff is all about
node's http client breaks this rule badly right now. will be fixed in 0.12!
Monday, April 15, 13
36. Unix Philosophy
•Interfaces should be obvious, simple, composible, consistent
•Callback => EventEmitter => Stream
•Portability, backwards compatibility
•"Every program is a proxy" - Ryan Dahl
•"Don't build big apps" - Substack
Monday, April 15, 13
38. Assemble rather than invent
•JavaScript is old
•Nonblocking I/O is ancient
•Unix is paleolithic
•Put proven things together in simple ways
•Unix philosophy applied to ideas
Monday, April 15, 13
40. Small Core, Small Parts
•The scope of node's core API is much smaller than
comparable server runtimes
•No glob, no xml, etc. This is on purpose.
•Small core => active userland
•"The Standard Library is where modules go to die" - Python
•Most npm modules are tiny, single-purpose, composible
Monday, April 15, 13
42. Node is a Community
•Everyone is an npm publisher. Everyone is a collaborator.
•Minimum Necessary Process: Governance mostly laissez-faire
•Small core team, BDFL model; Expansive prolific community
•Alternatives are cherished! No "one true" anything
•Work together to explore the solution space.
Monday, April 15, 13
47. JIFASNIF
•JavaScript is fun, and so Node is fun.
•When making a decision, always choose the funner option
•"Fun" is a very non-trivial concept! Not "easy" or "simple" or
"complicated" or "featureful" or "hard"
•A feeling of efficacy, rising to challenges, moving fast
•It's fun to create, share, put parts together, help each other
•Reduce obstacles to having fun!
Monday, April 15, 13
50. v1.0.0
•The next stable release after 0.12 (probably)
•Incremental improvements, uv/npm/V8 upgrades
•Not much in the way of new features
•Node-core's boundary is pretty much set
•The Node community's future is boundless
Monday, April 15, 13
51. See you on
the Internet!
http://j.mp/node-patterns-pdf
Monday, April 15, 13