SlideShare une entreprise Scribd logo
1  sur  31
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

Contenu connexe

Tendances

Webconf nodejs-production-architecture
Webconf nodejs-production-architectureWebconf nodejs-production-architecture
Webconf nodejs-production-architecture
Ben Lin
 
Node js quick tour v2
Node js quick tour v2Node js quick tour v2
Node js quick tour v2
Wyatt Fang
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
Tom Croucher
 

Tendances (19)

Построение простого REST сервера на Node.js | Odessa Frontend Code challenge
Построение простого REST сервера на Node.js | Odessa Frontend Code challengeПостроение простого REST сервера на Node.js | Odessa Frontend Code challenge
Построение простого REST сервера на Node.js | Odessa Frontend Code challenge
 
Introduction to node.js aka NodeJS
Introduction to node.js aka NodeJSIntroduction to node.js aka NodeJS
Introduction to node.js aka NodeJS
 
Node.js tutoria for beginner
Node.js tutoria for beginnerNode.js tutoria for beginner
Node.js tutoria for beginner
 
Webconf nodejs-production-architecture
Webconf nodejs-production-architectureWebconf nodejs-production-architecture
Webconf nodejs-production-architecture
 
Introduction to REST API with Node.js
Introduction to REST API with Node.jsIntroduction to REST API with Node.js
Introduction to REST API with Node.js
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Node js quick tour v2
Node js quick tour v2Node js quick tour v2
Node js quick tour v2
 
Node js for beginners
Node js for beginnersNode js for beginners
Node js for beginners
 
NodeJS - Server Side JS
NodeJS - Server Side JS NodeJS - Server Side JS
NodeJS - Server Side JS
 
Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming  Node.js and How JavaScript is Changing Server Programming
Node.js and How JavaScript is Changing Server Programming
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node.js - The New, New Hotness
Node.js - The New, New HotnessNode.js - The New, New Hotness
Node.js - The New, New Hotness
 
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, DhakaJavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013Node.js, for architects - OpenSlava 2013
Node.js, for architects - OpenSlava 2013
 
Introduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCatsIntroduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCats
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
node.js dao
node.js daonode.js dao
node.js dao
 

En vedette

En vedette (12)

America
AmericaAmerica
America
 
Jugando con websockets en nodeJS
Jugando con websockets en nodeJSJugando con websockets en nodeJS
Jugando con websockets en nodeJS
 
Cofty to china
Cofty to chinaCofty to china
Cofty to china
 
Republic of cuba by Olivia
Republic of cuba by OliviaRepublic of cuba by Olivia
Republic of cuba by Olivia
 
To Test or Not to Test?
To Test or Not to Test?To Test or Not to Test?
To Test or Not to Test?
 
Qa extreme 2011 eran kinsbruner
Qa extreme 2011 eran kinsbrunerQa extreme 2011 eran kinsbruner
Qa extreme 2011 eran kinsbruner
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Use Node.js to create a REST API
Use Node.js to create a REST APIUse Node.js to create a REST API
Use Node.js to create a REST API
 
Node js meetup
Node js meetupNode js meetup
Node js meetup
 
Introduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureIntroduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azure
 
เรียนรู้ Node JS แบบสบายๆ สำหรับผู้เริ่มต้น
เรียนรู้ Node JS แบบสบายๆ สำหรับผู้เริ่มต้นเรียนรู้ Node JS แบบสบายๆ สำหรับผู้เริ่มต้น
เรียนรู้ Node JS แบบสบายๆ สำหรับผู้เริ่มต้น
 
Node js
Node jsNode js
Node js
 

Similaire à GeekCampSG - Nodejs , Websockets and Realtime Web

Node js presentation
Node js presentationNode js presentation
Node js presentation
martincabrera
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Richard Lee
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
Sagiv Ofek
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
guileen
 

Similaire à GeekCampSG - Nodejs , Websockets and Realtime Web (20)

Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS Developing realtime apps with Drupal and NodeJS
Developing realtime apps with Drupal and NodeJS
 
Nodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredevNodejs a-practical-introduction-oredev
Nodejs a-practical-introduction-oredev
 
node.js: Javascript's in your backend
node.js: Javascript's in your backendnode.js: Javascript's in your backend
node.js: Javascript's in your backend
 
Node.js: A Guided Tour
Node.js: A Guided TourNode.js: A Guided Tour
Node.js: A Guided Tour
 
FITC - Node.js 101
FITC - Node.js 101FITC - Node.js 101
FITC - Node.js 101
 
Node.js 101 with Rami Sayar
Node.js 101 with Rami SayarNode.js 101 with Rami Sayar
Node.js 101 with Rami Sayar
 
node.js - Fast event based web application development
node.js - Fast event based web application developmentnode.js - Fast event based web application development
node.js - Fast event based web application development
 
Node js presentation
Node js presentationNode js presentation
Node js presentation
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
"You Don't Know NODE.JS" by Hengki Mardongan Sihombing (Urbanhire)
 
World of Node.JS
World of Node.JSWorld of Node.JS
World of Node.JS
 
Node.js: The What, The How and The When
Node.js: The What, The How and The WhenNode.js: The What, The How and The When
Node.js: The What, The How and The When
 
Why Nodejs Guilin Shanghai
Why Nodejs Guilin ShanghaiWhy Nodejs Guilin Shanghai
Why Nodejs Guilin Shanghai
 
Why Node.js
Why Node.jsWhy Node.js
Why Node.js
 
Node.js 1, 2, 3
Node.js 1, 2, 3Node.js 1, 2, 3
Node.js 1, 2, 3
 
End-to-end HTML5 APIs - The Geek Gathering 2013
End-to-end HTML5 APIs - The Geek Gathering 2013End-to-end HTML5 APIs - The Geek Gathering 2013
End-to-end HTML5 APIs - The Geek Gathering 2013
 
Exploring Node.jS
Exploring Node.jSExploring Node.jS
Exploring Node.jS
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

GeekCampSG - Nodejs , Websockets and Realtime Web

  • 1. Node.js , Socket.io and the Magic Bhagaban Behera
  • 2.
  • 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.
  • 5. Node.js - Features Really Lean( 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 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
  • 8.
  • 9.
  • 10. 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;
  • 11. 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
  • 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”);
  • 14.
  • 15. 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);
  • 16. 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/
  • 17. 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
  • 18. When to use Node.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.
  • 24. HTTP
  • 28.
  • 29. 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.
  • 30. Show me the code Visit http://socket.io/#how-to-use for excellent and simple documentation .