Node.js ,
Socket.io
   and
the Magic

     Bhagaban Behera
What is Node.js ?
Created by Ryan Dahl ( Joyent )

Command line tool to run Javascript

Built on Top of Google’s V8 Javascript Engine

Provides Javascript API for Network and File System . Used to
create Servers, Networking tools.

Event-driven , non-blocking I/O APIs

Core written in C/C++ . Supports C/C++ addons.

Can handle thousands of concurrent connections with minimal
overhead on a single process.
What’s the need ?
Node.js - Features

Really Lean( 8000 lines of C/C++
and 2000 lines of JS )
Exceptionally Fast
Easily Scalable
Event-Driven Approach
Analogy – Bank Reception

Analogy - Restaurant
The Cost of IO
Accessing RAM         ~250 CPU cycles

Disk Operations       ~41 000 000 CPU cycles
  Reading a file
  Writing to a file


Network IO            ~240 000 000 CPU cycles
  Database Query
  HTTP Responses
Handling I/O

 Apache is multithreaded (depending on
 conf.)
 Spawns a thread per request (or
 process)
 Each request is in its own "box”
 Blocking IO
result = query('SELECT * FROM ...');
print result.id;
The Node way
Single thread for your code
...but, I/O runs in parallel
Handle thousands of concurrent connections
with a single process
Need to be very careful with CPU-intensive
code
Why JavaScript ?

• Single Threaded , Asynchronous
• Non blocking IO
• Ubiquitous
• Massive codebase of libraries
• One language to rule them all( Full
  stack )
Hello World !
HellowWorld.js           HellowWorld.php



setTimeout(function(){   echo(“Hello”);

console.log(“World”);    sleep(2000);

},2000);                 echo(“World”);

console.log(“Hello”);
Our own HTTP Server
var http= require('http');

var s = http.createServer(function(req,res){

res.writeHead(200,{'content-type' : 'text/plain'}) ;

 res.end("hello world n");



})

s.listen(8000);
Built-in Modules

• File System
  require('fs');
• HTTP Client and Server
  require('http');
• TCP Sockets
  require('net');


• Many more: http://nodejs.org/docs/v0.4.8/api/
Express makes HTTP easier

• Node's HTTP module is low level.

• Express is a simple framework inspired by Sinatra
  (Ruby).

•   InstallingExpress:
    npm install express
• Robust verb-based routing
  app.get('/about', function(req, res) { });

• Templating(with Jade or EJS)

• Sessions
When to use Node.js ?
Realtime Apps

Crawlers

Single-page Apps

Streaming

File uploading

Online Gaming

Collaboration

Live Sports

Live chat

Financial Applications
Getting Started
Node.js: http://nodejs.org/#download
Buildinstructions: http://bit.ly/egLfzu Mac:
brew update && brew install node
npm, a package manager for Node:
curl http://npmjs.org/install.sh | sh
Many modules at: http://search.npmjs.org/
Windows support isn't great.
Node PAAS

NodeJitsu
Nodester
Joyent
Heroku
Socket.io
Real-time Web
History
Static Apps

Dynamic Apps

Ajax Apps

Real-time Apps
HTTP
Ajax Polling
Long Polling
Sockets
Websockets / Socket.io
Socket.IO aims to make realtime apps possible in every
browser and mobile device, blurring the differences between the
different transport mechanisms. It's care-free realtime 100% in
JavaScript.
Show me the code
Visit http://socket.io/#how-to-use   for excellent and
simple documentation .
Contact me !
bhagaban@noisestreet.com / @bhoga

GeekCampSG - Nodejs , Websockets and Realtime Web

  • 1.
    Node.js , Socket.io and the Magic Bhagaban Behera
  • 3.
    What is Node.js? Created by Ryan Dahl ( Joyent ) Command line tool to run Javascript Built on Top of Google’s V8 Javascript Engine Provides Javascript API for Network and File System . Used to create Servers, Networking tools. Event-driven , non-blocking I/O APIs Core written in C/C++ . Supports C/C++ addons. Can handle thousands of concurrent connections with minimal overhead on a single process.
  • 4.
  • 5.
    Node.js - Features ReallyLean( 8000 lines of C/C++ and 2000 lines of JS ) Exceptionally Fast Easily Scalable
  • 6.
    Event-Driven Approach Analogy –Bank Reception Analogy - Restaurant
  • 7.
    The Cost ofIO Accessing RAM ~250 CPU cycles Disk Operations ~41 000 000 CPU cycles Reading a file Writing to a file Network IO ~240 000 000 CPU cycles Database Query HTTP Responses
  • 10.
    Handling I/O Apacheis multithreaded (depending on conf.) Spawns a thread per request (or process) Each request is in its own "box” Blocking IO result = query('SELECT * FROM ...'); print result.id;
  • 11.
    The Node way Singlethread for your code ...but, I/O runs in parallel Handle thousands of concurrent connections with a single process Need to be very careful with CPU-intensive code
  • 12.
    Why JavaScript ? •Single Threaded , Asynchronous • Non blocking IO • Ubiquitous • Massive codebase of libraries • One language to rule them all( Full stack )
  • 13.
    Hello World ! HellowWorld.js HellowWorld.php setTimeout(function(){ echo(“Hello”); console.log(“World”); sleep(2000); },2000); echo(“World”); console.log(“Hello”);
  • 15.
    Our own HTTPServer var http= require('http'); var s = http.createServer(function(req,res){ res.writeHead(200,{'content-type' : 'text/plain'}) ; res.end("hello world n"); }) s.listen(8000);
  • 16.
    Built-in Modules • FileSystem require('fs'); • HTTP Client and Server require('http'); • TCP Sockets require('net'); • Many more: http://nodejs.org/docs/v0.4.8/api/
  • 17.
    Express makes HTTPeasier • Node's HTTP module is low level. • Express is a simple framework inspired by Sinatra (Ruby). • InstallingExpress: npm install express • Robust verb-based routing app.get('/about', function(req, res) { }); • Templating(with Jade or EJS) • Sessions
  • 18.
    When to useNode.js ? Realtime Apps Crawlers Single-page Apps Streaming File uploading Online Gaming Collaboration Live Sports Live chat Financial Applications
  • 19.
    Getting Started Node.js: http://nodejs.org/#download Buildinstructions:http://bit.ly/egLfzu Mac: brew update && brew install node npm, a package manager for Node: curl http://npmjs.org/install.sh | sh Many modules at: http://search.npmjs.org/ Windows support isn't great.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 29.
    Websockets / Socket.io Socket.IOaims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript.
  • 30.
    Show me thecode Visit http://socket.io/#how-to-use for excellent and simple documentation .
  • 31.