+ All Categories
Home > Technology > Node.js - Eine praktische Einführung (v7)

Node.js - Eine praktische Einführung (v7)

Date post: 15-Jan-2015
Category:
Upload: felix-geisendoerfer
View: 4,117 times
Download: 2 times
Share this document with a friend
Description:
Meine Slides vom 21.05.2011 bei der devcon Hamburg.Der code von der demo danach ist hier: https://gist.github.com/9f551235cdab4301e780
48
node.js Eine praktische Einführung 21.05.2011 (v7) Felix Geisendörfer
Transcript
Page 1: Node.js - Eine praktische Einführung (v7)

node.jsEine praktische Einführung

21.05.2011 (v7)

Felix Geisendörfer

Page 2: Node.js - Eine praktische Einführung (v7)

@felixge

Twitter / GitHub / IRC

Page 3: Node.js - Eine praktische Einführung (v7)

Datei Uploads & Umwandlung als Infrastruktur Service für Web- & Mobile-Apps.

- The app we run in production

Page 4: Node.js - Eine praktische Einführung (v7)

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

Page 5: Node.js - Eine praktische Einführung (v7)

node @ devcon?

Who is doing JavaScript?Has used node?Is using node in production (an app that is running this very moment)?

Page 6: Node.js - Eine praktische Einführung (v7)

“Easy” and “Scalable” : )

Node makes Hard -> Easy, Easy -> Hard

Page 7: Node.js - Eine praktische Einführung (v7)

Node.js

• Gestarted durch Ryan Dahl

• Google’s V8 JavaScript engine (kein DOM)

• Modul System, Dateisystem, Netzwerk, Http und Dns

Page 8: Node.js - Eine praktische Einführung (v7)

Installing

$ git clone \git://github.com/joyent/node.git$ cd node$ ./configure$ make install

Page 9: Node.js - Eine praktische Einführung (v7)

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

Page 10: Node.js - Eine praktische Einführung (v7)

Serverseitiges JavaScript

Page 11: Node.js - Eine praktische Einführung (v7)

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

Page 12: Node.js - Eine praktische Einführung (v7)

Was war das Problem?

• Langsame Engines

• Wenig Interesse an JavaScript bis ~2005

• Bessere alternativen

Page 13: Node.js - Eine praktische Einführung (v7)

Warum JavaScript?3 Gründe

Why not python / ruby / java

Page 14: Node.js - Eine praktische Einführung (v7)

#1 - Die Guten Seiten

World’s most misunderstood programming languageClosuresJSONFlexible

Page 15: Node.js - Eine praktische Einführung (v7)

#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

Page 16: Node.js - Eine praktische Einführung (v7)

#3 - Kein I/O im core

Page 17: Node.js - Eine praktische Einführung (v7)

Non-blocking I/O

Page 18: Node.js - Eine praktische Einführung (v7)

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

Page 19: Node.js - Eine praktische Einführung (v7)

I/O im Verhältnis

• CPU / Ram: Sehr Schnell

• Disk / Netzwerk: 1.000x - 1.000.000x langsamer

Page 20: Node.js - Eine praktische Einführung (v7)

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)

Page 21: Node.js - Eine praktische Einführung (v7)

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

Page 22: Node.js - Eine praktische Einführung (v7)

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);

Page 23: Node.js - Eine praktische Einführung (v7)

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?

Page 24: Node.js - Eine praktische Einführung (v7)

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?

Page 25: Node.js - Eine praktische Einführung (v7)

Anwendungsbereiche

Page 26: Node.js - Eine praktische Einführung (v7)

DIRT

• Data Intensive

• Real Time

Daten verlieren = schlechtDaten verzögern = noch schlechter

Page 27: Node.js - Eine praktische Einführung (v7)

Beispiele (DIRT)

• Chat Server

• Twitter

• Telephony

Page 28: Node.js - Eine praktische Einführung (v7)

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

Page 29: Node.js - Eine praktische Einführung (v7)

Interessante Projekte

Page 30: Node.js - Eine praktische Einführung (v7)

Package management

Page 31: Node.js - Eine praktische Einführung (v7)

2000+ Module in npm

+10 neue / Tag

Page 32: Node.js - Eine praktische Einführung (v7)

Web Frameworks

• Express.js (Sinatra clone)

• Fab.js (Mind-bending & awesome)

Page 33: Node.js - Eine praktische Einführung (v7)

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

Page 34: Node.js - Eine praktische Einführung (v7)

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

Page 35: Node.js - Eine praktische Einführung (v7)

Socket.IO

• WebSocket

• Adobe® Flash® Socket

• AJAX long polling

• AJAX multipart streaming

• Forever Iframe

• JSONP Polling

Chooses most capable transport at runtime!

Page 36: Node.js - Eine praktische Einführung (v7)

Ready for production?

Page 37: Node.js - Eine praktische Einführung (v7)

Unsere Erfahrungen

• Über 200.000 Datei Uploads

• Mehrere TB an Daten verarbeitet

• Keine bugs, Memory usage 30 - 80 MB

mit transloadit.com

Page 38: Node.js - Eine praktische Einführung (v7)

Community

Page 39: Node.js - Eine praktische Einführung (v7)

Benevolent Dictator For Life

Ryan Dahl

Wohlwollender Diktator auf Lebenszeit

Page 40: Node.js - Eine praktische Einführung (v7)

Community

• Mailing list (nodejs, nodejs-dev)

• IRC (#node.js) - 500+ User online

4000+ people on mailing list500+ people on IRC

Page 41: Node.js - Eine praktische Einführung (v7)

Hosting

Page 42: Node.js - Eine praktische Einführung (v7)

Probleme

Page 43: Node.js - Eine praktische Einführung (v7)

db.query('SELECT A', function() { db.query('SELECT B', function() { db.query('SELECT C', function() { db.query('SELECT D', function() { // WTF }); }); });});

Page 44: Node.js - Eine praktische Einführung (v7)

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

Page 45: Node.js - Eine praktische Einführung (v7)

Questions?

Page 46: Node.js - Eine praktische Einführung (v7)

Mehr Infos

nodeguide.com

nodebeginner.org/

Page 47: Node.js - Eine praktische Einführung (v7)

Node.js Workshop in Köln

nodecologne.eventbrite.com

15% Discount mit code ‘devcon’

Freitag, 10. Juni

Page 48: Node.js - Eine praktische Einführung (v7)

Questions?


Recommended