NODE.JS

The New kid in the block


DEfinition


  • Development platform built on top of Google V8
  • Focused on building server-side applications in Javascript
  • Event based
  • Asynchronous I/O
  • Differ from traditional models
    Receive - Process - Send - Wait - Receive


history


  • Ryan Dahl
  • Google V8 engine
  • Birth
  • Maturity
  • Ecosystem


hello World!


 var http = require('http'); http.createServer(function(request, response) {
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
 }).listen(8888);




Functional programming

Nouns vs Verbs




Function scope

Beware of callback marsh!


event driven asynchronous callbacks


       Synchronous model
 var result = database.query("SELECT * FROM hugetable");

console.log("Hello World");


       Asynchronous model
 database.query("SELECT * FROM hugetable", function(rows) {     var result = rows;     . . .  }); console.log("Hello World");


CPU vs cache vs memory vs i/o

  • 3 GHz processor runs 3x109 instructions / sec
  • 1 instruction          ===   1/3rdof a nanosecond
  • L1 and L2 cache   ===    2 - 5 ns
  • RAM                             ===    80 ns

Cat vs Blue Whale

  • var = "foo"  versus DB query?



EVent loop

  • Event model a core feature of the language
  • All I/O made non-blocking
  • Cascading callbacks
  • Only one thing happening at once



analogies

  • Ordering Books - Call back when available
  • Mom cooking in the kitchen - event based
  • Waiting area of a reception in a Hospital - non blocking



scalability

One instance per user 
versus
One instance for all users



STrategies

  • Once setup is complete, make all actions event-driven
  • If CPU intensive operation needed, delegate to web workers




deploying for production

How to leverage multiple CPUs and huge volume of RAM?



cluster

  • Delegate tasks to child processes
  • Sharing sockets with children
  • Master - Slave
  • Direct interaction with I/O - no bottleneck

Express

Web framework for Node to write web applications in MVC

Templating Libraries

  • EJS (Embedded JS)
  • Jade
  • Haml
  • JQuery templates
 var express = require('express');
 var app = express.createServer();
 app.get('/', function(req, res) {
     res.send('Welcome to Node');
 });
 app.listen(8000);

socket.io

  • Server - Browser persistent connection
  • Efficient low level socket mechanism
  • Shared interface between server and client
  • Named so because of support for HTML5 websockets
    • Web Socket
    • WebSocket over Flash
    • XHR Polling
    • XHR Multipart Streaming
    • Forever Iframe
    • JSONP Polling
  • Define namespaces by dividing listeners into channels
  • Data passed as JS objects between both endpoints

other useful modules

  • events
  • http
  • dns
  • vm
  • crypto
  • assert
  • child_process
  • mongoose
  • nodedb
  • sequelize
  • amqp
  • meteor and derby


ADDONS

Dynamically linked shared C/C++ libraries
 #include <v8.h>
 using namespace v8;
 extern "C" void init (Handle target) {
     HandleScope scope;
     target->Set(String::New("hello"), String::New("world"));
 }

// Equivalent to exports.hello = "world"


Node package manager

npm search <packagename>
npm install <package>
npm link <package>

npm init (Create a package)
npm adduser (Register at npmjs.org)
npm publish



Thank you!

NODE.JSThe New kid in the block

By shrikrishna

NODE.JSThe New kid in the block

  • 1,641