node.js is an evented server-side Javascript framework powered by the Google V8 Javascript engine. It is a platform ideal for creating highly scalable web applications. It has the same simplicity of frameworks such as Sinatra, but is designed to be more peformant from the ground up. This performance is achieved by making all network I/O non blocking and all file I/O asynchronous. We will go over how that impacts the development experience, and walk through a simple web application. Javascript is foundational to this type of I/O because it is already evented by design. We will also take a brief look a similar evented frameworks such as ruby`s EventMachine.
6. Y SO BAD?
•Slow Engines
•Javascript’s Perception (until recently)
•Much better alternatives
7. Y IZ BETTR NAO?
Lots of Competition
•SpiderMonkey
•JavascriptCore
•Chakra
Javascript is cool now!
8.
9. WHAT IS NODE.JS?
•Created By Ryan Dahl
•Google’s V8 Engine (No DOM)
•Uses Nonblocking I/O
•Single Threaded
•A New Way To Build Scalable Network Platforms
10. WHY SHOULD YOU CARE?
•Its Fast
> summary(node1$ttime)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.0000 0.0000 1.0000 0.7437 1.0000 106.0000
> summary(thin1$ttime)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.000 1.000 1.000 1.122 1.000 74.000
> summary(narwhal1$ttime)
Min. 1st Qu. Median Mean 3rd Qu. Max.
15.00 22.00 23.00 23.74 24.00 88.00
> summary(v8cgi1$ttime)
Min. 1st Qu. Median Mean 3rd Qu. Max.
12.00 13.00 13.00 14.49 18.00 39.00
11. WHY SHOULD YOU CARE?
•It can handle LOTS of concurrent transactions
12. WHY SHOULD YOU CARE?
• Makes near real time things easy
• Its small
• Its Javascript
•(Second most used language on Github)
19. BLOCKING I/O
var a = db.query('SELECT A');
console.log('result a:', a);
var b = db.query('SELECT B');
console.log('result b:', b);
Time = SUM(A, B)
21. SINGLE THREADED!
You have to use callbacks!
db.query('SELECT A', function(result) {
object.mySlowCall(result, function(){
console.log(“my result”);
})
});
22. WHY ISN’T EVERYONE USING
NON-BLOCKING I/O?
There are cultural and infrastructural
reasons
23. CULTURAL BIAS
We’re taught I/O with this:
puts(“Enter your name:”)
var name = gets()
We’re taught to demand input and do
nothing until we have it.
27. JAVASCRIPT...
•Has had event loops from the beginning
•Anonymous functions, closures
•Single threaded
•The culture of Javascript embraces evented
programming
28. GREAT FOR
•Single Page Apps
•Realtime updates
•Processors/Crawlers
•Process Monitoring
•File Uploading
29. INSTALLING
OSX
$ brew install node
Linux
$ git clone ....
$ configure
$ make
Windows
Not Yet (version 0.6)
30. INSTALLING NPM
•Node Package Manager
•Similar to RubyGems, Python easy_install
$ curl http://npmjs.org/install.sh | sh
31. COMMON JS MODULES
hello.js
exports.world = function(){
return “Hello World”;
}
main.js
var hello = require(‘./hello’);
var sys = require(‘sys’);
sys.puts(hello.world());
$ node main.js #Hello World
32. EVENTS
{EventEmitter} = require ‘events’
emitter = new EventEmitter
emitter.on ‘foo’, -> console.log ‘bar’
emitter.emitusual new_monster event. And check out how much nicer our
emitting the ‘foo’
dependency graph has become!
http://pragprog.com/magazines/2011-08/content
Imagine No Dependencies
A lot of Node developers will tell you that attaching things to global rather
than exports is a no-no. And if you’re packaging your code as a library, or trying
to make your code reusable, then they’re right. But if you’re developing a
standalone application, then go ahead and let yourself declare a few globals.
33. QUEUEING AN EVENT
function longFunction(){};
process.nextTick(longFunction);
Call longfunction on next time through event loop
34. EMITTING AN EVENT
var events = require('events');
var eventEmitter = new events.EventEmitter();
eventEmitter.on('someOccurence', function(message){
console.log(message);
});
eventEmitter.emit('someOccurence', 'Something happened!');
40. COFFEESCRIPT
•“Little language that compiles to javascript”
•“It’s just javascript”
• More ruby-like
square = (x) -> x * x
41. COFFEESCRIPT
•“Little language that compiles to javascript”
•“It’s just javascript”
• More ruby-like
square = (x) -> x * x
square = function(x){
return x * x;
}