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

5,058 views

Published on

Meine Slides vom 21.05.2011 bei der devcon Hamburg.

Der code von der demo danach ist hier: https://gist.github.com/9f551235cdab4301e780

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,058
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
26
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

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

  1. 1. node.jsEine praktische Einführung Felix Geisendörfer 21.05.2011 (v7)
  2. 2. @felixgeTwitter / GitHub / IRC
  3. 3. Datei Uploads & Umwandlung als Infrastruktur Service für Web- & Mobile-Apps.- The app we run in production
  4. 4. 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
  5. 5. node @ devcon?Who is doing JavaScript?Has used node?Is using node in production (an app that is running this very moment)?
  6. 6. “Easy” and “Scalable” : )Node makes Hard -> Easy, Easy -> Hard
  7. 7. Node.js• Gestarted durch Ryan Dahl• Google’s V8 JavaScript engine (kein DOM)• Modul System, Dateisystem, Netzwerk, Http und Dns
  8. 8. Installing$ git clone git://github.com/joyent/node.git$ cd node$ ./configure$ make install
  9. 9. Ingredients libeio libev c-ares V8 http_parserlibev: event looplibeio: async I/Oc-ares: non-blocking dnshttp_parser: 40 bytes / connectionv8: google’s JS engine
  10. 10. Serverseitiges JavaScript
  11. 11. Serverseitiges JavaScript • Netscape LiveWire (1996) • Rhino (1997) • 50+ weitere PlattformenLiveWire to script enterprise appliancesRhino was done because the plan was to rewrite netscape entirely in Java (Javagator)Wikipedia lists 50+ other plattform since then
  12. 12. Was war das Problem?• Langsame Engines• Wenig Interesse an JavaScript bis ~2005• Bessere alternativen
  13. 13. Warum JavaScript? 3 GründeWhy not python / ruby / java
  14. 14. #1 - Die Guten SeitenWorld’s most misunderstood programming languageClosuresJSONFlexible
  15. 15. V8 (Chrome) SpiderMonkey (Firefox) JavaScriptCore (Safari) #2 - JS Engine Wars Chakra (IE9) Carakan (Opera) siehe: arewefastyet.comWar on terrorSpider Monkey: TraceMonkey, JägerMonkeyJavaScriptCore: SquirrelFish Extreme / Nitro
  16. 16. #3 - Kein I/O im core
  17. 17. Non-blocking I/O
  18. 18. 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
  19. 19. I/O im Verhältnis• CPU / Ram: Sehr Schnell• Disk / Netzwerk: 1.000x - 1.000.000x langsamer
  20. 20. Non-Blocking I/Odb.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)
  21. 21. 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
  22. 22. 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);
  23. 23. Single Threaded var a = []; function first() { a.push(1); • [1, 2, 3, 4] ? a.push(2); } • [3, 4, 1, 2] ? function second() { a.push(3); a.push(4); • [1, 3, 2, 4] ? } setTimeout(first, 10); • BAD_ACCESS ? setTimeout(second, 10);Who thinks:- Answer not on the list?- A possible?- B possible?- C possible?
  24. 24. Single Threaded var a = []; function first() { a.push(1); • [1, 2, 3, 4] a.push(2); } • [3, 4, 1, 2] function second() { a.push(3); a.push(4); • [1, 3, 2, 4] } setTimeout(first, 10); • BAD_ACCESS setTimeout(second, 10);Who thinks:- Answer not on the list?- A possible?- B possible?- C possible?
  25. 25. Anwendungsbereiche
  26. 26. DIRT • Data Intensive • Real TimeDaten verlieren = schlechtDaten verzögern = noch schlechter
  27. 27. Beispiele (DIRT)• Chat Server• Twitter• Telephony
  28. 28. Andere Beispiele • Single Page Apps • File uploads • Unix tools parallel laufen lassenhttp://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: itencourages large swaths of mediocre programmers to overengineer shit, and it keepssystems administrators in business by giving them something to respond to at 4AM.” -- TedDziuba
  29. 29. Interessante Projekte
  30. 30. Package management
  31. 31. 2000+ Module in npm +10 neue / Tag
  32. 32. Web Frameworks• Express.js (Sinatra clone)• Fab.js (Mind-bending & awesome)
  33. 33. 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
  34. 34. 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
  35. 35. Socket.IO • WebSocket • Adobe® Flash® Socket • AJAX long polling • AJAX multipart streaming • Forever Iframe • JSONP PollingChooses most capable transport at runtime!
  36. 36. Ready for production?
  37. 37. Unsere Erfahrungen mit transloadit.com• Über 200.000 Datei Uploads• Mehrere TB an Daten verarbeitet• Keine bugs, Memory usage 30 - 80 MB
  38. 38. Community
  39. 39. Benevolent Dictator For Life Ryan DahlWohlwollender Diktator auf Lebenszeit
  40. 40. Community • Mailing list (nodejs, nodejs-dev) • IRC (#node.js) - 500+ User online4000+ people on mailing list500+ people on IRC
  41. 41. Hosting
  42. 42. Probleme
  43. 43. db.query(SELECT A, function() { db.query(SELECT B, function() { db.query(SELECT C, function() { db.query(SELECT D, function() { // WTF }); }); });});
  44. 44. 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
  45. 45. Questions?
  46. 46. Mehr Infosnodeguide.com /nodebeginner.org
  47. 47. Node.js Workshop in Köln Freitag, 10. Juninodecologne.eventbrite.com 15% Discount mit code ‘devcon’
  48. 48. Questions?

×