Presentation made on January 2011 about node.js. This technology was used to be the main technology behind the API on "Guia VIVO TV" (codename TVSTAR) with MongoDB.
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
node.js - Eventful JavaScript on the Server
1. node.js - Eventful JavaScript on the Server
By David Ruiz - @wupsbr
Centro de Inovação do grupo Telefônica Brasil
2. What is node.js?
In a nutshell, it’s JavaScript on
the server side
Created by Ryan Dahl (@ryah) in
2009
Based on the Google V8
JavaScript engine + Evented I/O
Performance is king:
Property access through hidden
classes
Machine code
Garbage collection
3. Want a HTTP Server?
var http = require('http');
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to the HTTP Server!');
}).listen(4000);
4. Want a TCP Server?
var tcp = require('net');
tcp.createServer(function(socket) {
socket.addListener('connect', function() {
socket.write('Welcome to the TCP Server!n>');
});
}).listen(4000);
6. Evented I/O + V8 Engine
libeio: async I/O
libev: event loop
libuv: wrapper for libev and IOCP
There is a single thread running
No parallel execution... for YOUR code!
db.query().select('*').from('users').execute(function(){
fs.readFile('settings.json', function () {
// Block for five seconds :(
var now = new Date().getTime();
while(new Date().getTime() < now + 5000);
// Response :)
});
});
7. What about multiple cores?
The load balancer approach
:1337
:1338
:1339
The OS approach
var http = require('http'), cluster = require('cluster');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('TID Rocks!');
});
cluster(server).listen(1337);
8. And about packages?
One line install:
# curl http://npmjs.org/install.sh | sh
Now, let’s install the node.js mysql client?
# npm install mysql
9. And about packages?
One line install:
# curl http://npmjs.org/install.sh | sh
Now, let’s install the node.js mysql client?
There are more than 4900 packages, and more
# npm install mysql
than 15 are added each day!
10. Let’s check the performance
PHP NODE.JS
$a = null; var i, a, b, c, max;
$b = null;
$c = null; max = 1e6;
$i = null;
$max = 1e6; console.time('maths');
$start = microtime(true); for (i = 0; i < max; i++)
{
for ($i = 0; $i < $max; $i++) a = 1234 + 5678 + i;
{ b = 1234 * 5678 + i;
$a = 1234 + 5678 + $i; c = 1234 / 2 + i;
$b = 1234 * 5678 + $i; }
$c = 1234 / 2 + $i;
} console.timeEnd('maths');
var_dump(microtime(true) - $start);
http://www.matt-knight.co.uk/2011/node-js-vs-php-performance-maths/
11. Let’s check the performance
RESULTS IN SECONDS
PHP NODE.JS
$a = null; LOOP SIZE PHP var i, a, b, c, max; NODE.JS
$b = null;
$c = null; max = 1e6;
$i = null;
$max = 1e6; console.time('maths');
100.000 0,077 0,002
$start = microtime(true); for (i = 0; i < max; i++)
{
for ($i = 0; $i < $max; $i++) a = 1234 + 5678 + i;
{ 1.000.000 0,759 b = 1234 * 5678 + 0,016
i;
$a = 1234 + 5678 + $i; c = 1234 / 2 + i;
$b = 1234 * 5678 + $i; }
$c = 1234 / 2 + $i;
} console.timeEnd('maths');
10.000.000 7,605 0,157
var_dump(microtime(true) - $start);
100.000.000 75,159 1,567
http://www.matt-knight.co.uk/2011/node-js-vs-php-performance-maths/
12. Who are using node.js?
linkedin (m.linkedin.com) - mobile stack
The improvements the team saw were staggering. They went from running 15
servers with 15 instances (virtual servers) on each physical machine, to just 4
instances that can handle double the traffic.
http://venturebeat.com/2011/08/16/linkedin-node/
klout (klout.com) - frontend + api
In our tests, a single node.js process was able to handle thousands of
concurrent connections in a very CPU-efficient manner. Plus, using JavaScript
on both the server and the client made writing multi-purpose code very
straightforward. We knew of other companies using node.js at the time, but
most were using it to serve APIs.
http://corp.klout.com/blog/2011/10/the-tech-behind-klout-com/
and many others: HP Yahoo!, Nokia, Heroku, VMWare, Loopt ....
,
14. Cross platform socket
About Socket.IO Supported languages
Socket.IO aims to make realtime apps Cocoa (iOS)
- fpotter/socketio-cocoa
possible in every browser and mobile Erlang
device, blurring the differences - yrashk/socket.io-erlang
Flash
between the different transport - simb/FlashSocket.IO
mechanisms. It's care-free realtime Go
- madari/go-socket.io (Currently not compatible with 0.7+)
100% in JavaScript. Java
In order to provide realtime connectivity - ibdknox/socket.io-netty
- benkay/java-socket.io.client
on every browser, Socket.IO selects - Ovea/Socket.IO-Java
Lua
the most capable transport at runtime, - ignacio/LuaNode-Socket.IO
without it affecting the API: MrJoes/tornadio (0.6)
- gevent-socketio
WebSocket, Adobe® Flash® Socket, Perl
AJAX long polling, AJAX multipart - vti/pocketio
Python
streaming, Forever Iframe and JSONP - MrJoes/tornadio2 (0.7+)
Polling. Ruby
- markjeee/Socket.IO-rack
15. The node.js ecosystem
Express Cluster
A Sinatra inspired node.js web development Extensible multi-core server management for
framework. nodejs.
Jade Supervisor
Jade is a high performance template engine A little supervisor script for nodejs. It runs
heavily influenced by Haml and implemented your program, and watches for code
with JavaScript for node. changes, so you can have hot-code
Socket.io reloading-ish behavior, without worrying
An simple HTTP Socket interface about memory leaks.
implementation and server. Joyent (http://no.de)
Mongoose Free SmartMachine hosting to delivery
Mongoose aims at solving the complexities modern applications with nodejs.
associated with asynchronous data storage Expresso
by providing a more intuitive API. TDD for nodejs projects in express.
Testosterone
Another and simple TDD for nodejs.