NODE JS
by Gyuri Horak
WHAT IS NODE.JS
“Node has a clear purpose: provide an easy
way to build scalable network programs. It is
not a tool for every problem. Do not write a ray
tracer with Node. Do not write a web browser
with Node. Do however reach for Node if
tasked with writing a DNS server, DHCP
server, or even a video encoding server.”
Ryan Dahl
NODE.JS
● server-side javascript environment
● asynchronous event-driven model
● based on Google's V8 javascript engine
● open source, controlled by Joyent
● well documented
● lot of 3rd party modules
ASYNCHRONOUS?
● like Python's Twisted or Ruby's
EventMachine
● the event loop is hidden from the user
● non-blocking I/O (network, file, etc.)
● developers should think async
ASYNCHRONOUS!
mysql_select_db('db');
$result = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_array($result)) {
echo $row['data'];
}
echo "finished";
var mysql = require('mysql'),
client = mysql.createClient({
user: 'user',
password: 'password'
});
client.query('USE db');
client.query('SELECT * FROM table',
function selectCb(err, result, fields) {
console.log(result);
}
);
console.log("finished");
NO THREADS
● a node.js application is single threaded
● child_process.fork()
● I/O doesn't block, but you can:
for (var i = 0; i < 10000; i++) {
heavy_calculate_PI();
}
for (var i = 0; i < 10000; i++) {
setTimeout(heavy_calculate_PI, 0);
}
MODULES
● CommonJS Modules proposal
var hello = exports;
hello.say = function (name) { console.log("Hello "+name); }
var hello = require('./hello');
hello.say('Sanyi'); // "Hello Sanyi!"
● vs. AMD [Asynchronous Module Definition]
MODULES
● npm - node package manager
● more then 1000 listed on the wiki
● ~5000 repositories on github related to node.js
Web frameworks (MVC like fws, static file servers,
middlewares), database drivers (mysql, pgsql, sqlite,
mongodb, ...), templating, css engines, CMSs, SSL/crypto
fws, SMTP, TCP/IP, RPC, web socket, message queues,
class systems, testing fws, parsers (json, xml, command
line, parser generators), debugging utilities, compression,
graphics, payment gateways, i18n/l10n, coffeescript, full
text search, ...
HTTP MODULE
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/');
$ node hello.js
Server running at http://127.0.0.1:1337/
SPEED
● Google V8 (used in Chrome as well), JIT
● may perform better then not optimized C++
(performance test)
● ~25 times faster than PHP (~10* CPython,
~2.5* PyPy)
● http module outperforms apache, sometimes
even nginx
● typical setup: multiple node.js instances
running behind an nginx (or node.js) proxy
SPEED OF HELLO.JS
50000 requests
● 1 client: 2211 r/s (apache: 2991) (nginx: 4009)
● 100 clients: 8088 r/s (apache: 12520) (nginx: 14021)
● 1000 clients: 6924 r/s (apache: crbp, 5693, 1356 failed)
(nginx: 11603, error: 235)
● 10000 clients: 5786 r/s (C10k problem?) (apache: crbp,
3956, 6421 failed) (nginx: 5991, error: 23137)
● 20000 clients: 4559 r/s ... (apache: crbp, 2399, 9552
failed) (nginx: -)
jsdom
● DOM implementation for node.js
● almost everything works
● YUI 3, jQuery, ...
● unit testing web applications
● rendering and manipulating content on
server side
HOSTING
● easy to install
○ very few dependecies
○ ./configure && make
● easy to manage
○ no need for webserver
● Lot of managed node.js hosting providers:
Heroku, no.de, CloudFoundry, dotCloud, ...
EXAMPLE PROJECTS
● Calipso - CMS based on node.js
● 64squares - Real time chess on facebook
● Hummingbird - real time visitor statistics
● Kraken.io - image optimizer
● Word2 - MMO word game
● ...

Node.js

  • 1.
  • 2.
    WHAT IS NODE.JS “Nodehas a clear purpose: provide an easy way to build scalable network programs. It is not a tool for every problem. Do not write a ray tracer with Node. Do not write a web browser with Node. Do however reach for Node if tasked with writing a DNS server, DHCP server, or even a video encoding server.” Ryan Dahl
  • 3.
    NODE.JS ● server-side javascriptenvironment ● asynchronous event-driven model ● based on Google's V8 javascript engine ● open source, controlled by Joyent ● well documented ● lot of 3rd party modules
  • 4.
    ASYNCHRONOUS? ● like Python'sTwisted or Ruby's EventMachine ● the event loop is hidden from the user ● non-blocking I/O (network, file, etc.) ● developers should think async
  • 5.
    ASYNCHRONOUS! mysql_select_db('db'); $result = mysql_query("SELECT* FROM table"); while ($row = mysql_fetch_array($result)) { echo $row['data']; } echo "finished"; var mysql = require('mysql'), client = mysql.createClient({ user: 'user', password: 'password' }); client.query('USE db'); client.query('SELECT * FROM table', function selectCb(err, result, fields) { console.log(result); } ); console.log("finished");
  • 6.
    NO THREADS ● anode.js application is single threaded ● child_process.fork() ● I/O doesn't block, but you can: for (var i = 0; i < 10000; i++) { heavy_calculate_PI(); } for (var i = 0; i < 10000; i++) { setTimeout(heavy_calculate_PI, 0); }
  • 7.
    MODULES ● CommonJS Modulesproposal var hello = exports; hello.say = function (name) { console.log("Hello "+name); } var hello = require('./hello'); hello.say('Sanyi'); // "Hello Sanyi!" ● vs. AMD [Asynchronous Module Definition]
  • 8.
    MODULES ● npm -node package manager ● more then 1000 listed on the wiki ● ~5000 repositories on github related to node.js Web frameworks (MVC like fws, static file servers, middlewares), database drivers (mysql, pgsql, sqlite, mongodb, ...), templating, css engines, CMSs, SSL/crypto fws, SMTP, TCP/IP, RPC, web socket, message queues, class systems, testing fws, parsers (json, xml, command line, parser generators), debugging utilities, compression, graphics, payment gateways, i18n/l10n, coffeescript, full text search, ...
  • 9.
    HTTP MODULE var http= require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, "127.0.0.1"); console.log('Server running at http://127.0.0.1:1337/'); $ node hello.js Server running at http://127.0.0.1:1337/
  • 10.
    SPEED ● Google V8(used in Chrome as well), JIT ● may perform better then not optimized C++ (performance test) ● ~25 times faster than PHP (~10* CPython, ~2.5* PyPy) ● http module outperforms apache, sometimes even nginx ● typical setup: multiple node.js instances running behind an nginx (or node.js) proxy
  • 11.
    SPEED OF HELLO.JS 50000requests ● 1 client: 2211 r/s (apache: 2991) (nginx: 4009) ● 100 clients: 8088 r/s (apache: 12520) (nginx: 14021) ● 1000 clients: 6924 r/s (apache: crbp, 5693, 1356 failed) (nginx: 11603, error: 235) ● 10000 clients: 5786 r/s (C10k problem?) (apache: crbp, 3956, 6421 failed) (nginx: 5991, error: 23137) ● 20000 clients: 4559 r/s ... (apache: crbp, 2399, 9552 failed) (nginx: -)
  • 12.
    jsdom ● DOM implementationfor node.js ● almost everything works ● YUI 3, jQuery, ... ● unit testing web applications ● rendering and manipulating content on server side
  • 13.
    HOSTING ● easy toinstall ○ very few dependecies ○ ./configure && make ● easy to manage ○ no need for webserver ● Lot of managed node.js hosting providers: Heroku, no.de, CloudFoundry, dotCloud, ...
  • 14.
    EXAMPLE PROJECTS ● Calipso- CMS based on node.js ● 64squares - Real time chess on facebook ● Hummingbird - real time visitor statistics ● Kraken.io - image optimizer ● Word2 - MMO word game ● ...