node.js - eine praktische einführung (v7)
DESCRIPTION
Meine Slides vom 21.05.2011 bei der devcon Hamburg.Der code von der demo danach ist hier: https://gist.github.com/9f551235cdab4301e780TRANSCRIPT
node.jsEine praktische Einführung
21.05.2011 (v7)
Felix Geisendörfer
@felixge
Twitter / GitHub / IRC
Datei Uploads & Umwandlung als Infrastruktur Service für Web- & Mobile-Apps.
- The app we run in production
Core Contributor
&
Module Author
node-mysql node-formidable
- Joined the mailing list in June 26, 2009- When I joined there where #24 people- First patch in September 2009- Now mailing list has > 3400 members
node @ devcon?
Who is doing JavaScript?Has used node?Is using node in production (an app that is running this very moment)?
“Easy” and “Scalable” : )
Node makes Hard -> Easy, Easy -> Hard
Node.js
• Gestarted durch Ryan Dahl
• Google’s V8 JavaScript engine (kein DOM)
• Modul System, Dateisystem, Netzwerk, Http und Dns
Installing
$ git clone \git://github.com/joyent/node.git$ cd node$ ./configure$ make install
Ingredients
V8
libev
http_parser
c-ares
libeio
libev: event looplibeio: async I/Oc-ares: non-blocking dnshttp_parser: 40 bytes / connectionv8: google’s JS engine
Serverseitiges JavaScript
Serverseitiges JavaScript
• Netscape LiveWire (1996)
• Rhino (1997)
• 50+ weitere Plattformen
LiveWire to script enterprise appliances
Rhino was done because the plan was to rewrite netscape entirely in Java (Javagator)
Wikipedia lists 50+ other plattform since then
Was war das Problem?
• Langsame Engines
• Wenig Interesse an JavaScript bis ~2005
• Bessere alternativen
Warum JavaScript?3 Gründe
Why not python / ruby / java
#1 - Die Guten Seiten
World’s most misunderstood programming languageClosuresJSONFlexible
#2 - JS Engine Wars
V8 (Chrome)
Chakra (IE9)
SpiderMonkey (Firefox) JavaScriptCore (Safari)
Carakan (Opera)
siehe: arewefastyet.comWar on terror
Spider Monkey: TraceMonkey, JägerMonkeyJavaScriptCore: SquirrelFish Extreme / Nitro
#3 - Kein I/O im core
Non-blocking I/O
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);
Dauer = SUM(A, B)
Disk / Network access is 1.000-1.000.000 slower than CPU / Ram access
I/O im Verhältnis
• CPU / Ram: Sehr Schnell
• Disk / Netzwerk: 1.000x - 1.000.000x langsamer
Non-Blocking I/O
db.query('SELECT A', function(result) { console.log('result a:', result);});
db.query('SELECT B', function(result) { console.log('result b:', result);});
Dauer = MAX(A, B)
Non-Blocking I/O
• I/O Aufgaben an den Kernel weitergeben und events durch file descriptor polling erkannt (select, epoll, kqueue, etc.)
• Thread pool für alles andere
Single Threadedvar a = [];function first() { a.push(1); a.push(2);}
function second() { a.push(3); a.push(4);}
setTimeout(first, 10);setTimeout(second, 10);
Single Threaded
• [1, 2, 3, 4] ?
• [3, 4, 1, 2] ?
• [1, 3, 2, 4] ?
• BAD_ACCESS ?
var a = [];function first() { a.push(1); a.push(2);}
function second() { a.push(3); a.push(4);}
setTimeout(first, 10);setTimeout(second, 10);
Who thinks:
- Answer not on the list?- A possible?- B possible?- C possible?
Single Threaded
• [1, 2, 3, 4]
• [3, 4, 1, 2]
• [1, 3, 2, 4]
• BAD_ACCESS
var a = [];function first() { a.push(1); a.push(2);}
function second() { a.push(3); a.push(4);}
setTimeout(first, 10);setTimeout(second, 10);
Who thinks:
- Answer not on the list?- A possible?- B possible?- C possible?
Anwendungsbereiche
DIRT
• Data Intensive
• Real Time
Daten verlieren = schlechtDaten verzögern = noch schlechter
Beispiele (DIRT)
• Chat Server
• Telephony
Andere Beispiele
• Single Page Apps
• File uploads
• Unix tools parallel laufen lassen
http://teddziuba.com/2011/02/the-case-against-queues.html
“I love asynchronous stuff as much as the next guy, but think of a queue like Java: it encourages large swaths of mediocre programmers to overengineer shit, and it keeps systems administrators in business by giving them something to respond to at 4AM.” -- Ted Dziuba
Interessante Projekte
Package management
2000+ Module in npm
+10 neue / Tag
Web Frameworks
• Express.js (Sinatra clone)
• Fab.js (Mind-bending & awesome)
Socket.IOvar http = require('http');var io = require('socket.io');
var server = http.createServer();server.listen(80);
var socket = io.listen(server);socket.on('connection', function(client){ client.send('hi');
client.on('message', function(){ … }) client.on('disconnect', function(){ … })});
Server
Socket.IO
<script src="http://{node_server_url}/socket.io/socket.io.js" /><script> var socket = new io.Socket({node_server_url}); socket.connect(); socket.on('connect', function(){ … }) socket.on('message', function(){ … }) socket.on('disconnect', function(){ … })</script>
Client
Socket.IO
• WebSocket
• Adobe® Flash® Socket
• AJAX long polling
• AJAX multipart streaming
• Forever Iframe
• JSONP Polling
Chooses most capable transport at runtime!
Ready for production?
Unsere Erfahrungen
• Über 200.000 Datei Uploads
• Mehrere TB an Daten verarbeitet
• Keine bugs, Memory usage 30 - 80 MB
mit transloadit.com
Community
Benevolent Dictator For Life
Ryan Dahl
Wohlwollender Diktator auf Lebenszeit
Community
• Mailing list (nodejs, nodejs-dev)
• IRC (#node.js) - 500+ User online
4000+ people on mailing list500+ people on IRC
Hosting
Probleme
db.query('SELECT A', function() { db.query('SELECT B', function() { db.query('SELECT C', function() { db.query('SELECT D', function() { // WTF }); }); });});
Probleme
• Stack traces gehen an der event loop Grenze verloren
• Aufgaben auf mehrere Prozesse verteilen
• V8: 1- 1.9 GB heap limit / GC Probleme
+ not as many libraries as the ruby ecosystem
Questions?
☝
Mehr Infos
nodeguide.com
nodebeginner.org/
Node.js Workshop in Köln
nodecologne.eventbrite.com
15% Discount mit code ‘devcon’
Freitag, 10. Juni
Questions?
☝