Successfully reported this slideshow.

Node.js - As a networking tool

19

Share

Loading in …3
×
1 of 47
1 of 47

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Node.js - As a networking tool

  1. 1. Node.js As a networking tool 28.12.2010
  2. 2. About Contributor Co-founder Felix Geisendörfer node.js driver node-formidable
  3. 3. Who are you?
  4. 4. Node.js?
  5. 5. JavaScript?
  6. 6. Port = 80 ?
  7. 7. Port != 80 ?
  8. 8. Node's goal is to provide an easy way to build scalable network programs.
  9. 9. Node.js • Created by Ryan Dahl • Google’s V8 JavaScript engine (no DOM) • Module system, I/O bindings, Common Protocols
  10. 10. N et w or k Hello World Ed it io n $ cat hello.js var net = require('net'); net.createServer(function(socket) { socket.write('hello worldn'); socket.end(); }).listen(0x27C3); $ node hello.js & [1] 3591 $ nc localhost 10179 hello world
  11. 11. Why JavaScript? 3 Reasons
  12. 12. #1 - The good parts
  13. 13. V8 (Chrome) SpiderMonkey (Firefox) JavaScriptCore (Safari) #2 - JS Engine Wars Chakra (IE9) Carakan (Opera)
  14. 14. #3 - No I/O or Threads
  15. 15. Non-blocking I/O
  16. 16. Blocking I/O db.query('SELECT A'); console.log('query A done'); db.query('SELECT B'); console.log('query B done'); Time = SUM(A, B)
  17. 17. Non-Blocking I/O db.query('SELECT A', function(result) { console.log('query A done'); }); db.query('SELECT B', function(result) { console.log('query B done'); }); Time = MAX(A, B)
  18. 18. libev by Marc Lehmann
  19. 19. libev • An infinite loop (event loop) • Polls the OS for events (select, epoll, kqueue, ...) • Allows you to register watchers / callbacks for the events you care about
  20. 20. Buffers
  21. 21. Buffers • Raw C memory allocation • Similar to an array of integers • Can be converted to and from JS strings
  22. 22. Buffers buffer.write('Hello Buffer'); buffer.toString('utf-8'); buffer[0] = 23; buffer.slice(10, 20);
  23. 23. Reverse Hello World $ cat reverse_hello.js var net = require('net'); net.createServer(function(socket) { socket.on('data', function(buffer) { console.log(buffer); }); }).listen(0x27C3); $ node reverse_hello.js & [1] 3523 $ echo "hello world" | nc localhost 10179 <Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64 0a>
  24. 24. Streams
  25. 25. “Streams are to time as arrays are to space.” -- Jed Schmidt @ JSConf.eu
  26. 26. Streams stream.write(new Buffer([0x27, 0xC3]); stream.pause(); stream.resume(); stream.destroy();
  27. 27. Streams stream .on('drain', function() {}) .on('data', function(buffer) {}) .on('end', function() {});
  28. 28. Streams $ cat echo.js var net = require('net'); net.createServer(function(socket) { socket.pipe(socket); }).listen(0x27C3); $ node echo.js & [1] 6207 $ nc 127.0.0.1 10179 Hi, how are you? Hi, how are you?
  29. 29. pcap module Sniffing data with node.js
  30. 30. pcap module • Node.js bindings for libpcap • Simple install: `npm install pcap` • “Scriptable tcpdump”
  31. 31. pcap module $ cat pcap.js var util = require('util'); var pcap = require('pcap'); var filter = 'tcp port '+0x27C3; var session = pcap.createSession('lo0', filter); session.on('packet', function(packet) { packet = pcap.decode.packet(packet); console.log(pcap.print.packet(packet)); if (packet.link.ip.tcp.data) { util.print(packet.link.ip.tcp.data.toString()); } });
  32. 32. pcap module $ node hello.js > /dev/null & [1] 3592 $ sudo node pcap.js & [2] 3593 $ nc 127.0.0.1 10179 > /dev/null loopback 127.0.0.1:54078 -> 127.0.0.1:10179 TCP len 64 [syn] loopback 127.0.0.1:10179 -> 127.0.0.1:54078 TCP len 64 [ack,syn] loopback 127.0.0.1:54078 -> 127.0.0.1:10179 TCP len 52 [ack] loopback 127.0.0.1:10179 -> 127.0.0.1:54078 TCP len 64 [ack,psh] hello world loopback 127.0.0.1:54078 -> 127.0.0.1:10179 TCP len 52 [ack]
  33. 33. Recap • Binary data handling (Buffers) • I/O abstraction (Streams) • Packet sniffing (node-pcap)
  34. 34. Node can also do • HTTP • Child processes • Unix domain sockets • DNS • UDP • Crypto • ...
  35. 35. Speed
  36. 36. Speed var http = require(’http’) var b = new Buffer(1024*1024); http.createServer(function (req, res) { res.writeHead(200); res.end(b); }).listen(8000); by Ryan Dahl
  37. 37. Speed 100 concurrent clients 1 megabyte response node 822 req/sec nginx 708 thin 85 mongrel 4 (bigger is better) by Ryan Dahl
  38. 38. Speed • NGINX peaked at 4mb of memory • Node peaked at 60mb • Very special circumstances by Ryan Dahl
  39. 39. Danger, Will Robinson
  40. 40. Danger, Will Robinson Node makes it trivial to manage thousands of connections on multiple protocol levels.
  41. 41. File uploading & processing as a service for other web applications.
  42. 42. Questions? ✎ @felixge / felix@debuggable.com
  43. 43. Questions? ✎ @felixge / felix@debuggable.com
  44. 44. Bonus Slides!
  45. 45. Addons
  46. 46. Addons • Add bindings to your favorite C/C++ libraries • Can be written as a mix of C++ / JavaScript
  47. 47. Addons #include <v8.h> #include <node.h> #include <unistd.h> using namespace v8; static Handle<Value> Fork(const Arguments& args) { HandleScope scope; pid_t pid = fork(); if (pid < 0) { return ThrowException(Exception::Error(String::New("fork: could not fork, pid < 0. see man fork"))); } if (pid == 0) { ev_default_fork(); } return scope.Close(Number::New(pid)); } extern "C" void init (Handle<Object> target) { HandleScope scope; NODE_SET_METHOD(target, "fork", Fork); }

Editor's Notes

  • \n
  • - Joined the mailing list in June 26, 2009\n- Co-Transloadit\n- When I joined there where #24 people, which in this room we can round to 23 : )\n- First patch in September 2009\n- Now we have &gt; 3200 members\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • &amp;#x201C;Easy&amp;#x201D; and &amp;#x201C;Scalable&amp;#x201D; : )\n\nNode makes Hard -&gt; Easy, Easy -&gt; Hard\n
  • \n
  • Ported to C this turns into 100-200 lines of code\n
  • Why not python / ruby / java\n
  • World&amp;#x2019;s most misunderstood programming language\nClosures\nJSON\nFlexible\n
  • Spider Monkey: TraceMonkey, J&amp;#xE4;gerMonkey\nJavaScriptCore: SquirrelFish Extreme / Nitro\n
  • \n
  • \n
  • Disk / Network access is 1.000-1.000.000 slower than CPU / Ram access\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • - At 103 characters perfectly tweet sized\n
  • \n
  • \n
  • \n
  • \n
  • All you need for networking adventures\n
  • \n
  • \n
  • \n
  • ~Benchmark Ryan Dahl did: 6 month ago\n
  • Anti Benchmark: Huge String\n
  • \n
  • \n
  • - 2013: 90% will be internet video\n- Transloadit can help you handling the upload and encoding\n- Built node.js\n
  • Mention podcast with Tim Pritlove\n
  • \n
  • \n
  • \n
  • \n
  • ×