KEMBAR78
Understanding the Node.js Platform | PDF
understanding
                                       the

                                  platform
Domenic Denicola
http://domenicdenicola.com
@domenic
story time




@domenic
Domenic Denicola
                                          @domenic
           https://npmjs.org/profile/domenicdenicola
                          http://github.com/domenic
                          http://github.com/NobleJS



@domenic
agenda


 how      to node
 why      to node
 coding     time



@domenic
how to node




@domenic
how to node




@domenic
how to node




@domenic
why to node


 new      and shiny
 fast
 scalable
 low-level
 community

@domenic
new and shiny




@domenic
let’s look at the most-used
  node.js packages.


@domenic
   socket.io: used by 306 other packages
    redis: 259 (hiredis: 70)
    stylus: 148 (less: 134)
    mongodb: 144 (mongoose: 135)




@domenic
fast




@domenic
@domenic
New HTTP Parser
 I've implemented a new HTTP/1.1 request and response parser by hand. (My
 previous parser was written with the help of Ragel.) It requires 124 bytes per
 HTTP connection, makes zero allocations, has no dependencies, is nearly optimal
 in its use of CPU instructions, interruptible on any character, has extensive tests,
 and is MIT licensed.

 README

 http_parser.h

 http_parser.c



 (Only one user at the moment: I've just merged it into Node.)



                                         http://four.livejournal.com/1033160.html
@domenic
@domenic
scalable




@domenic
q: what do web servers actually do?

  a: i/o


@domenic
Response.Write("hello, world!");




@domenic
move_uploaded_file(
     $_FILES['userphoto']['tmp_name'],
     "/var/www/userphotos/" . $_POST['username']
  );




@domenic
import memcache

  mc = memcache.Client(['127.0.0.1:11211'])

  mc.set("heavily_used_data", "data")
  value = mc.get("more_data")



@domenic
class Post < ActiveRecord::Base
     attr_accessible :content, :name, :title

    validates :name, :presence => true
    validates :title, :presence => true
  end



@domenic
move this stuff out of the context of
  the web for a moment



@domenic
@domenic
q: how do last-generation web
     servers fix this problem?

  a: threads
@domenic
let’s talk about threads.


@domenic
they suck.

  the end.

@domenic
q: how does node solve this problem?


  a: javascript
@domenic
My next project
 I'm going to write a special thin web server tied to the V8 javascript interpreter
 The web server will execute javascripts in response to requests in real-time.
 The beautiful thing about this is that I can lock the users in an evented box where they have no choice
 but to be fast. They cannot touch the disk. They cannot access a database with some stupid library that
 blocks because they have no access to blocking I/O. They cannot resize an image by linking imagemagick
 into the web server process (and thus slowing down/blocking the entire thing). They cannot crash it
 because they are very limited in what they can do.
 Why javascript?
    because its bare and does not come with I/O APIs
    web developers use it already
    DOM API is event-based. Everyone is already used to running without threads and on an event loop
     already.
 I think this design will be extremely efficient and support very high loads. Web requests are transformed
 into mysql, memcached, AMQP requests and then return to the event loop.
 Web developers need this sort of environment where it is not possible for them to do stupid things. Ruby,
 python, C++, PHP are all terrible languages for web development because they allow too much freedom.




                                                      http://four.livejournal.com/963421.html
@domenic
javascript has never had
  blocking i/o


@domenic
javascript has never had
  more than one thread


@domenic
instead we use callbacks
  and events


@domenic
$.get("http://nodejs.org", function (data) {
      document.body.innerHTML = data;
  });




@domenic
var dbReq = indexedDB.open("my-database");

  dbReq.addEventListener("success", function () {
      var dbConnection = dbReq.result;
  });




@domenic
it’s the same in node


@domenic
fs.readFile("/etc/passwd", function (err, data) {
      console.log(data);
  });




@domenic
request.on("data", function (chunk) {
      response.write(chunk);
  });




@domenic
db.users.find({ name: "domenic" }, function (err, users) {
      users.forEach(function (user) {
          response.write(user);
      });
  });




@domenic
io.sockets.on("connection", function (socket) {
      socket.emit("news", { hello: "world" });
      socket.on("my other event", function (data) {
        console.log(data);
      });
  });



@domenic
low-level




@domenic
 STDIO  Timers  Process  Utilities
    Events  Domain  Buffer  Stream
     Crypto  TLS/SSL  String Decoder
 File System  Path  Net  UDP/Datagram
 DNS  HTTP  HTTPS  URL  Query Strings
     Punycode  Readline  REPL  VM
  Child Processes  Assertion Testing  TTY
             ZLIB  OS  Cluster


                        http://nodejs.org/docs/latest/api/
@domenic
that’s it.

  that’s all you get.

@domenic
community




@domenic
@domenic
codingTime();
            https://github.com/domenic/understanding-node




@domenic

Understanding the Node.js Platform

  • 1.
    understanding the platform Domenic Denicola http://domenicdenicola.com @domenic
  • 2.
  • 3.
    Domenic Denicola @domenic https://npmjs.org/profile/domenicdenicola http://github.com/domenic http://github.com/NobleJS @domenic
  • 4.
    agenda how to node why to node coding time @domenic
  • 5.
  • 6.
  • 7.
  • 8.
    why to node new and shiny fast scalable low-level community @domenic
  • 9.
  • 10.
    let’s look atthe most-used node.js packages. @domenic
  • 11.
    socket.io: used by 306 other packages  redis: 259 (hiredis: 70)  stylus: 148 (less: 134)  mongodb: 144 (mongoose: 135) @domenic
  • 12.
  • 13.
  • 14.
    New HTTP Parser I've implemented a new HTTP/1.1 request and response parser by hand. (My previous parser was written with the help of Ragel.) It requires 124 bytes per HTTP connection, makes zero allocations, has no dependencies, is nearly optimal in its use of CPU instructions, interruptible on any character, has extensive tests, and is MIT licensed. README http_parser.h http_parser.c (Only one user at the moment: I've just merged it into Node.) http://four.livejournal.com/1033160.html @domenic
  • 15.
  • 16.
  • 17.
    q: what doweb servers actually do? a: i/o @domenic
  • 18.
  • 19.
    move_uploaded_file( $_FILES['userphoto']['tmp_name'], "/var/www/userphotos/" . $_POST['username'] ); @domenic
  • 20.
    import memcache mc = memcache.Client(['127.0.0.1:11211']) mc.set("heavily_used_data", "data") value = mc.get("more_data") @domenic
  • 21.
    class Post <ActiveRecord::Base attr_accessible :content, :name, :title validates :name, :presence => true validates :title, :presence => true end @domenic
  • 22.
    move this stuffout of the context of the web for a moment @domenic
  • 23.
  • 24.
    q: how dolast-generation web servers fix this problem? a: threads @domenic
  • 25.
    let’s talk aboutthreads. @domenic
  • 26.
    they suck. the end. @domenic
  • 27.
    q: how doesnode solve this problem? a: javascript @domenic
  • 28.
    My next project I'm going to write a special thin web server tied to the V8 javascript interpreter The web server will execute javascripts in response to requests in real-time. The beautiful thing about this is that I can lock the users in an evented box where they have no choice but to be fast. They cannot touch the disk. They cannot access a database with some stupid library that blocks because they have no access to blocking I/O. They cannot resize an image by linking imagemagick into the web server process (and thus slowing down/blocking the entire thing). They cannot crash it because they are very limited in what they can do. Why javascript?  because its bare and does not come with I/O APIs  web developers use it already  DOM API is event-based. Everyone is already used to running without threads and on an event loop already. I think this design will be extremely efficient and support very high loads. Web requests are transformed into mysql, memcached, AMQP requests and then return to the event loop. Web developers need this sort of environment where it is not possible for them to do stupid things. Ruby, python, C++, PHP are all terrible languages for web development because they allow too much freedom. http://four.livejournal.com/963421.html @domenic
  • 29.
    javascript has neverhad blocking i/o @domenic
  • 30.
    javascript has neverhad more than one thread @domenic
  • 31.
    instead we usecallbacks and events @domenic
  • 32.
    $.get("http://nodejs.org", function (data){ document.body.innerHTML = data; }); @domenic
  • 33.
    var dbReq =indexedDB.open("my-database"); dbReq.addEventListener("success", function () { var dbConnection = dbReq.result; }); @domenic
  • 34.
    it’s the samein node @domenic
  • 35.
    fs.readFile("/etc/passwd", function (err,data) { console.log(data); }); @domenic
  • 36.
    request.on("data", function (chunk){ response.write(chunk); }); @domenic
  • 37.
    db.users.find({ name: "domenic"}, function (err, users) { users.forEach(function (user) { response.write(user); }); }); @domenic
  • 38.
    io.sockets.on("connection", function (socket){ socket.emit("news", { hello: "world" }); socket.on("my other event", function (data) { console.log(data); }); }); @domenic
  • 39.
  • 40.
     STDIO Timers  Process  Utilities  Events  Domain  Buffer  Stream  Crypto  TLS/SSL  String Decoder  File System  Path  Net  UDP/Datagram  DNS  HTTP  HTTPS  URL  Query Strings  Punycode  Readline  REPL  VM  Child Processes  Assertion Testing  TTY  ZLIB  OS  Cluster http://nodejs.org/docs/latest/api/ @domenic
  • 41.
    that’s it. that’s all you get. @domenic
  • 42.
  • 43.
  • 44.
    codingTime(); https://github.com/domenic/understanding-node @domenic

Editor's Notes

  • #9 Don’t expand on each point yet
  • #10 “Solving modern problems.”
  • #18 Ask the audience what their webservers doLead in to next slide is “let’s look at some examples of this in our usual web frameworks”
  • #25 Ask the audience what their webservers do
  • #28 Ask the audience what their webservers do
  • #29 Don’t explain this, just read it---explanation is on upcoming slides.
  • #42 Why is this a good thing? Well that leads us to our next point, which is that Node.js is…
  • #44 Don’t forget Knox and Q stories