Professional Documents
Culture Documents
js
A Guided Tour
Presenter: C. Aaron Cois, Ph.D.
Real-time Interaction
Real-time Interaction
Real-Time
Games
Chat
Stock
persistent I/O
HTTP wasnt built for this Servers need to push data to clients Polling is slow and inefficient
Outline
What
is Node.js? Technology Overview How does it work? Demo Code! Deployment and Hosting
What is Node.js?
Node.js
Node.js
Most
importantly, node.js is a
server-side runtime environment, that compiles and executes JavaScript very efficiently.
Platforms
Runs
Clickable
Linux
Why Node?
Node.js
is specifically designed for building fast, efficient, scalable network applications Node uses an event-driven, non-blocking I/O model to maximize efficiency
Technology: V8 Engine
Developed by Google Ships with the Google Chrome web browser Allows Chrome to run JavaScript code much faster
It does this by compiling the JavaScript directly into native machine code As opposed to interpreting JavaScript, or execute it as bytecode
Technology: JavaScript
JavaScript
is:
of doing anything other traditional languages (C++, Java, Ruby, etc) can do
Has an excellent event model Traditionally resigned to the context of the web application frontend
i.e.
Technology: JavaScript2
Theres
This is where node.js comes in, executing JavaScript efficiently on the server-side
JavaScript
brings to Node.js:
Natural event-based programming, ideal for client-server applications A known language, with low overhead
I/O is expensive
*http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
Fork
Threads
Another Thesis
Thread-per-connection is memory-expensive
worker is now in use, and can handle no other connection, even if it is waiting on:
File
Waiting on DB
Waiting on DB
Thread 1
Thread 2
Thread 3
Thread 4
Time
Thread 1
Thread 2
Thread 3
Thread 4
Time
Thread 1
Thread 2
Time
Even worse
If
all threads are in use, every incoming connection is blocked can cause massive traffic jams on high-throughput applications
This
There is another
Axiom:
Multi-Threaded code
Is difficult to write Is difficult to debug Sucks up more dev/test/maintenance cycles Most often has inefficient performance
Conclusion:
Single threaded?!?
Skeptical? I dont blame you
Instead
DB I/O command to event loop Net I/O command to event loop File I/O command to event loop Time
DB I/O command to event loop Net I/O command to event loop File I/O command to event loop Time
run entirely in a single thread Passes I/O requests to the event loop, along with callbacks
Your
code then:
Goes to sleep Uses no system resources Will be notified via callback when I/O is complete
Callback example
var filename = test_file.txt; fs.open(filename, w, function(err, file) { if (err) throw err; });
Callback example
Filesystem module forwards task to event loop
var file = (test_file.txt); fs.open(file, w, function(err, file) { if (err) throw err; });
Callback example
Callback is invoked when work is complete
var file = (test_file.txt); fs.open(file, w, function(err, file) { if (err) throw err; });
following:
Node is in charge
Let
Node.js handle
What
To not block when you tell it to Order of execution (e.g. forked parallel processes)
Interlude: Modules
Node.js API
Crypto
Streams Many more
Cryptography
STDIO
Node.js Community
Additionally,
the Node.js community maintains excellent modules to enhance the capabilities of Node modules operate as
These
Jade
Connect Less
}).listen(1337, '127.0.0.1');
Live Demo
http://techfestchat.jit.su
has been expending a lot of effort to make Node a first class framework
And now
A sneak preview!
MS WebMatrix 2
Open WebMatrix 2
Cloud Hosting
Heroku Microsoft Azure Nodejitsu Cloud Foundry Nodester DotCloud Appfog Joyent (coming soon)
Thanks!
If you are interested in formal Node.js learning, I also have a course available at:
http://www.udemy.com/learn-nodejs-byexample/