SlideShare a Scribd company logo
www.edureka.co/mastering-node-js
View Mastering Node.js course details at www.edureka.co/mastering-node-js
Communication in Node.js
Slide 2Slide 2Slide 2Slide 2 www.edureka.co/mastering-node-js
 Introduction of Node.js
 Use Cases of Node.js
 Network Communication in Node.js
 Two Way Communication in Node.js
What will you learn today?
Slide 3 www.edureka.co/mastering-node-jsSlide 3
Traditional Multithreaded Model
Slide 4 www.edureka.co/mastering-node-jsSlide 4
Problem with Multithreaded Model
Thread 1 Thread 3
Shared
Resource
wants to
update
wants to
update
Thread 2
wants to
update
 In a multi-threaded HTTP server, for each and every request that the server receives, it creates a separate
thread which handles that request
 If a request acquires a lock in the shared resource and it is ‘exclusive’, it will affect result of other requests
Slide 5 www.edureka.co/mastering-node-jsSlide 5
Single Threading
Event
Loop
Event
Queue
Thread Pool
file system
network
process
other
One Thread at a Time
 On the other hand, framework like Node.js is event driven, handling all requests asynchronously from single
thread
 Almost no function in Node directly performs I/O, so the process never blocks
Slide 6 www.edureka.co/mastering-node-jsSlide 6
What is Node.js ?
 Node.js is a server-side runtime environment for networking applications. It is single threaded.
 Node.js applications are written in JavaScript, and its open source based on Google’s V8 JavaScript Engine
 It is cross platform and can run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD,
NonStop and IBM
Slide 7 www.edureka.co/mastering-node-jsSlide 7
Node.js Detailed Architecture
Client 1
Request
1
Request
1
Blocking
IO
Request
?
Client 2
Client n
Request
2
Request
n
Request
2
Event Loop
Single
Threaded
Non Blocking I/O
Tasks Processed
here
Request 1
Request 2
Request n
Response 1
Response 2
Response n
No
No
Non-Blocking IO
Non-Blocking IO
Request
1
Request
2
Send
Responses
Pick up requests from queue
Event Queue
Slide 8 www.edureka.co/mastering-node-jsSlide 8
Node.js Detailed Architecture
Client 1
Request
1
T-1
Request
1
Database
Filesystem
Blocking
IO
Request
?
Client 2
Client n
Request
2
Request
n
T-2 T-m
Thread
T-1
Request
n
Request
2
Request
n
Event Loop
Single
Threaded
Non Blocking I/O
Tasks Processed
here
Request 1
Request 2
Request n
Response 1
Response 2
Response n
No
No
Yes
handle by
Non-Blocking IO
Non-Blocking IO
Request
1
Request
2
Send
Responses
Pick up requests from queue
Node.js Platform internal Thread PoolEvent Queue
Send Response to Request n
Pick up one
Thread from
pool
Blocking IO
Slide 9 www.edureka.co/mastering-node-jsSlide 9
Use-Cases of Node.js (Contd.)
 Previously LinkedIn Mobile was powered by Ruby on Rails
 It was a synchronous app in which clients used to make several calls
for a single page, consequently the system was bursting because of
the load
Slide 10 www.edureka.co/mastering-node-jsSlide 10
Use-Cases of Node.js (Contd.)
 Previously LinkedIn Mobile was powered by Ruby on Rails
 It was a synchronous app in which clients used to make several calls
for a single page, consequently the system was bursting because of
the load
Advantage of using Node.js
 The LinkedIN moved to Node.js which enabled them to move to a
model where the client makes a single request for a page
 It led to reduction in the number of machines used to host their
services greater than 10:1
Slide 11 www.edureka.co/mastering-node-jsSlide 11
Use-Cases of Node.js (Contd.)
 Ebay was making real time applications with huge number of eBay-
specific services
 As eBay was having java infrastructure, it consumed many more
resources than expected, raising questions about scalability for
production
Slide 12 www.edureka.co/mastering-node-jsSlide 12
 Ebay was making real time applications with huge number of eBay-
specific services
 As eBay was having java infrastructure, it consumed many more
resources than expected, raising questions about scalability for
production
 Node.js solved this issue as it is scalable because its single
threaded so less overheads and that is why it utilizes the resources
in the right manner, so no extra consumption of resources
Use-Cases of Node.js (Contd.)
Advantage of using Node.js
Slide 13 www.edureka.co/mastering-node-jsSlide 13
Uber’s Story
 Uber is a transportation company which allows users to get a taxi, private car or rideshare from their smartphones
Uber uses NodeJS to implement their cab dispatch operation
Slide 14 www.edureka.co/mastering-node-jsSlide 14
Uber - Old Architecture
PHP
PHP
PHP
PHP
Slide 15 www.edureka.co/mastering-node-jsSlide 15
Uber - Multiple Dispatch Problem
 Since PHP is a multithreaded language , each user’s request is handled in a separate thread
 Once one car is dispatched for a user, in between the same car get dispatched to another user
 Reason was car dispatch operation was executed from multiple threads
Slide 16 www.edureka.co/mastering-node-jsSlide 16
Uber - New Architecture
iPhone
Android
NodeJS
Dispatch
(NodeJS)
Dispatch State
(MongoDB)
Real-time Logic
Business Logic
Persistent Store
(MySQL)
Python
Slide 17 www.edureka.co/mastering-node-jsSlide 17
Job Trends
From the graph below : The number of jobs are skyrocketing.
Let us see how communication
works in Node.js
Slide 19 www.edureka.co/mastering-node-jsSlide 19Slide 19Slide 19
 For Network communication on Node.js, we use the “net” module
 net.createServer([options][,callback]) :
 If allowHalfOpen is true then when the other side initiates Connection Termination the server WILL NOT send the FIN
packet
Network Communication - TCP
//options object
{
allowHalfOpen : false , pauseOnConnect : false
}
//values : true or false, default : false
Slide 20 www.edureka.co/mastering-node-jsSlide 20Slide 20Slide 20
//Creating a TCP Server
var net = require(‘net’);
var server = net.createServer(function(socket)
{
socket.end(‘Hello World’); //Socket is a Duplex stream
});
server.listen(3000,function()
{
console.log(“Server is listening on Port 3000”);
});
Network Communication - TCP
Slide 21 www.edureka.co/mastering-node-jsSlide 21Slide 21Slide 21
//A TCP client
var net = require(‘net’);
var socket = net.createConnection({port: 3000,host: ‘192.168.0.1’);
socket.on(‘connect’,function()
{
console.log(‘connected to the server’);
})
socket.end(‘Hello Server’);
//we can now create a command line TCP chat server and client by using the //process.stdin
(Readable Stream) and process.stdout(Writable)
Network Communication - TCP
Demo
Slide 23 www.edureka.co/mastering-node-jsSlide 23Slide 23Slide 23
Two Way Communication : Socket.io
 Socket.io is a fast, real-time engine.
 Transmitting messages and Receiving message between client and server is simple: Events.
 On server/client when sending a message use socket.emit(‘eventname’,data). “eventname” can be any string
 And data can be any data: even Binary data!
 On server/client when you want to listen to events-messages use socket.on(‘eventname’,callbackFunction).
 Where the callbackFunction is a function that accepts a Data argument : Data sent by the other party.
Slide 24 www.edureka.co/mastering-node-jsSlide 24Slide 24Slide 24
 A simple example:
//Server-side
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
//client-side
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Two Way Communication : Socket.io
Slide 25 www.edureka.co/mastering-node-jsSlide 25Slide 25Slide 25
 Besides ‘connect’, ‘message’ and ‘disconnect’ you can use any custom event names
 You could also have a separation of concerns by namespacing. Namespacing also means, that the same
websocket connection is used but is multiplexed
var io = require('socket.io').listen(80);
var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only'
, '/chat': 'will get'
}); });
var news = io
.of('/news')
.on('connection', function (socket) {
socket.emit('item', { news: 'item' });
});
<script>
var chat = io.connect('http://localhost/chat')
, news = io.connect('http://localhost/news');
chat.on('connect', function () {
chat.emit('hi!');
});
news.on('news', function () {
news.emit('woot');
});
</script>
Two Way Communication : Socket.io
Demo
Slide 27Slide 27Slide 27Slide 27 www.edureka.co/mastering-node-js
Certifications
Get certified in Mastering Node.js by Edureka
Edureka's Mastering Node.js course:
• You will learn how to develop fast real-time network applications using Node.js, ExpressJS and MongoDB, deal with templating
engines like Jade/Hogan/Handlebars and understand testing using Mocha/Jasmine
• It will train you to build networking and web based applications that are far more superior and efficient than applications build
in other languages.
• Get to work on a To-Do List App Project towards the end of the course, which gives you complete insights on the Node.js
framework.
• Online Live Courses: 27 hours
• Assignments: 20 hours
• Project: 20 hours
• Lifetime Access + 24 X 7 Support
Go to www.edureka.co/mastering-node-js
Thank You
Questions/Queries/Feedback
Recording and presentation will be made available to you within 24 hours

More Related Content

KEY
OSCON 2011 - Node.js Tutorial
PDF
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
PDF
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
PDF
Node.js Explained
PPT
Nodejs Event Driven Concurrency for Web Applications
PDF
Building web apps with node.js, socket.io, knockout.js and zombie.js - Codemo...
PDF
Building servers with Node.js
PDF
Original slides from Ryan Dahl's NodeJs intro talk
OSCON 2011 - Node.js Tutorial
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
What is Node.js | Node.js Tutorial for Beginners | Node.js Modules | Node.js ...
Node.js Explained
Nodejs Event Driven Concurrency for Web Applications
Building web apps with node.js, socket.io, knockout.js and zombie.js - Codemo...
Building servers with Node.js
Original slides from Ryan Dahl's NodeJs intro talk

What's hot (20)

KEY
Writing robust Node.js applications
PPTX
Introduction to Node.js
PDF
NodeJS: an Introduction
PPT
RESTful API In Node Js using Express
PPTX
Introduction to Node js
PDF
Nodejs vatsal shah
PDF
Modern UI Development With Node.js
PPTX
NodeJS - Server Side JS
PPTX
Introduction to node.js
PDF
All aboard the NodeJS Express
PDF
NodeJS
PDF
Server Side Event Driven Programming
PPTX
Java script at backend nodejs
KEY
A million connections and beyond - Node.js at scale
PDF
Node Architecture and Getting Started with Express
PPTX
Intro to Node.js (v1)
PDF
NodeJS: the good parts? A skeptic’s view (devnexus2014)
PDF
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
PDF
Writing RESTful web services using Node.js
Writing robust Node.js applications
Introduction to Node.js
NodeJS: an Introduction
RESTful API In Node Js using Express
Introduction to Node js
Nodejs vatsal shah
Modern UI Development With Node.js
NodeJS - Server Side JS
Introduction to node.js
All aboard the NodeJS Express
NodeJS
Server Side Event Driven Programming
Java script at backend nodejs
A million connections and beyond - Node.js at scale
Node Architecture and Getting Started with Express
Intro to Node.js (v1)
NodeJS: the good parts? A skeptic’s view (devnexus2014)
JavaScript as a Server side language (NodeJS): JSConf 2011, Dhaka
Writing RESTful web services using Node.js
Ad

Viewers also liked (18)

PPTX
Handling web servers of high traffic sites
PDF
Go at uber
PDF
Go and Uber’s time series database m3
PDF
Node Foundation Membership Overview 20160907
PPTX
Node js for enterprise
PDF
Bonnes pratiques de développement avec Node js
PPT
Mobile Data Terminal M710W Presentation
PPTX
Wso2 con 2014 event driven architecture Publish/Subscribe Pubsub
KEY
OSCON 2012 MongoDB Tutorial
PPTX
Kafka + Uber- The World’s Realtime Transit Infrastructure, Aaron Schildkrout
PDF
NodeJS基礎教學&簡介
PPTX
Tutorial on Question Answering Systems
PDF
用十分鐘將你的網站送上雲端
PPTX
Siddhi: A Second Look at Complex Event Processing Implementations
PPTX
Talk on Industrial Internet of Things @ Intelligent systems tech forum 2014
PDF
Create Restful Web Application With Node.js Express Framework
PDF
The Enterprise Case for Node.js
PDF
SlideShare 101
Handling web servers of high traffic sites
Go at uber
Go and Uber’s time series database m3
Node Foundation Membership Overview 20160907
Node js for enterprise
Bonnes pratiques de développement avec Node js
Mobile Data Terminal M710W Presentation
Wso2 con 2014 event driven architecture Publish/Subscribe Pubsub
OSCON 2012 MongoDB Tutorial
Kafka + Uber- The World’s Realtime Transit Infrastructure, Aaron Schildkrout
NodeJS基礎教學&簡介
Tutorial on Question Answering Systems
用十分鐘將你的網站送上雲端
Siddhi: A Second Look at Complex Event Processing Implementations
Talk on Industrial Internet of Things @ Intelligent systems tech forum 2014
Create Restful Web Application With Node.js Express Framework
The Enterprise Case for Node.js
SlideShare 101
Ad

Similar to Communication in Node.js (20)

PDF
NodeJS : Communication and Round Robin Way
PDF
Day In A Life Of A Node.js Developer
PDF
Day in a life of a node.js developer
ODP
Introduce about Nodejs - duyetdev.com
PDF
Comet with node.js and V8
PPTX
Introduction to Node.JS
PPTX
Node.js: A Guided Tour
PPT
Node js
PPTX
Real World Lessons on the Pain Points of Node.JS Application
PDF
soft-shake.ch - Hands on Node.js
PDF
Why Node.js
PDF
Why Nodejs Guilin Shanghai
PDF
Developing realtime apps with Drupal and NodeJS
PPTX
Building Applications With the MEAN Stack
PPT
Socket Programming - nitish nagar
PPTX
Create simple api using node js
PDF
Nodejs and WebSockets
PPTX
PDF
Virtual Threads in Java: A New Dimension of Scalability and Performance
PPTX
node.js.pptx
NodeJS : Communication and Round Robin Way
Day In A Life Of A Node.js Developer
Day in a life of a node.js developer
Introduce about Nodejs - duyetdev.com
Comet with node.js and V8
Introduction to Node.JS
Node.js: A Guided Tour
Node js
Real World Lessons on the Pain Points of Node.JS Application
soft-shake.ch - Hands on Node.js
Why Node.js
Why Nodejs Guilin Shanghai
Developing realtime apps with Drupal and NodeJS
Building Applications With the MEAN Stack
Socket Programming - nitish nagar
Create simple api using node js
Nodejs and WebSockets
Virtual Threads in Java: A New Dimension of Scalability and Performance
node.js.pptx

More from Edureka! (20)

PDF
What to learn during the 21 days Lockdown | Edureka
PDF
Top 10 Dying Programming Languages in 2020 | Edureka
PDF
Top 5 Trending Business Intelligence Tools | Edureka
PDF
Tableau Tutorial for Data Science | Edureka
PDF
Python Programming Tutorial | Edureka
PDF
Top 5 PMP Certifications | Edureka
PDF
Top Maven Interview Questions in 2020 | Edureka
PDF
Linux Mint Tutorial | Edureka
PDF
How to Deploy Java Web App in AWS| Edureka
PDF
Importance of Digital Marketing | Edureka
PDF
RPA in 2020 | Edureka
PDF
Email Notifications in Jenkins | Edureka
PDF
EA Algorithm in Machine Learning | Edureka
PDF
Cognitive AI Tutorial | Edureka
PDF
AWS Cloud Practitioner Tutorial | Edureka
PDF
Blue Prism Top Interview Questions | Edureka
PDF
Big Data on AWS Tutorial | Edureka
PDF
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
PDF
Kubernetes Installation on Ubuntu | Edureka
PDF
Introduction to DevOps | Edureka
What to learn during the 21 days Lockdown | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Tableau Tutorial for Data Science | Edureka
Python Programming Tutorial | Edureka
Top 5 PMP Certifications | Edureka
Top Maven Interview Questions in 2020 | Edureka
Linux Mint Tutorial | Edureka
How to Deploy Java Web App in AWS| Edureka
Importance of Digital Marketing | Edureka
RPA in 2020 | Edureka
Email Notifications in Jenkins | Edureka
EA Algorithm in Machine Learning | Edureka
Cognitive AI Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Blue Prism Top Interview Questions | Edureka
Big Data on AWS Tutorial | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Kubernetes Installation on Ubuntu | Edureka
Introduction to DevOps | Edureka

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Electronic commerce courselecture one. Pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Encapsulation theory and applications.pdf
Encapsulation_ Review paper, used for researhc scholars
The Rise and Fall of 3GPP – Time for a Sabbatical?
Dropbox Q2 2025 Financial Results & Investor Presentation
Network Security Unit 5.pdf for BCA BBA.
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Electronic commerce courselecture one. Pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Building Integrated photovoltaic BIPV_UPV.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
NewMind AI Monthly Chronicles - July 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Chapter 3 Spatial Domain Image Processing.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Encapsulation theory and applications.pdf

Communication in Node.js

  • 1. www.edureka.co/mastering-node-js View Mastering Node.js course details at www.edureka.co/mastering-node-js Communication in Node.js
  • 2. Slide 2Slide 2Slide 2Slide 2 www.edureka.co/mastering-node-js  Introduction of Node.js  Use Cases of Node.js  Network Communication in Node.js  Two Way Communication in Node.js What will you learn today?
  • 3. Slide 3 www.edureka.co/mastering-node-jsSlide 3 Traditional Multithreaded Model
  • 4. Slide 4 www.edureka.co/mastering-node-jsSlide 4 Problem with Multithreaded Model Thread 1 Thread 3 Shared Resource wants to update wants to update Thread 2 wants to update  In a multi-threaded HTTP server, for each and every request that the server receives, it creates a separate thread which handles that request  If a request acquires a lock in the shared resource and it is ‘exclusive’, it will affect result of other requests
  • 5. Slide 5 www.edureka.co/mastering-node-jsSlide 5 Single Threading Event Loop Event Queue Thread Pool file system network process other One Thread at a Time  On the other hand, framework like Node.js is event driven, handling all requests asynchronously from single thread  Almost no function in Node directly performs I/O, so the process never blocks
  • 6. Slide 6 www.edureka.co/mastering-node-jsSlide 6 What is Node.js ?  Node.js is a server-side runtime environment for networking applications. It is single threaded.  Node.js applications are written in JavaScript, and its open source based on Google’s V8 JavaScript Engine  It is cross platform and can run within the Node.js runtime on OS X, Microsoft Windows, Linux, FreeBSD, NonStop and IBM
  • 7. Slide 7 www.edureka.co/mastering-node-jsSlide 7 Node.js Detailed Architecture Client 1 Request 1 Request 1 Blocking IO Request ? Client 2 Client n Request 2 Request n Request 2 Event Loop Single Threaded Non Blocking I/O Tasks Processed here Request 1 Request 2 Request n Response 1 Response 2 Response n No No Non-Blocking IO Non-Blocking IO Request 1 Request 2 Send Responses Pick up requests from queue Event Queue
  • 8. Slide 8 www.edureka.co/mastering-node-jsSlide 8 Node.js Detailed Architecture Client 1 Request 1 T-1 Request 1 Database Filesystem Blocking IO Request ? Client 2 Client n Request 2 Request n T-2 T-m Thread T-1 Request n Request 2 Request n Event Loop Single Threaded Non Blocking I/O Tasks Processed here Request 1 Request 2 Request n Response 1 Response 2 Response n No No Yes handle by Non-Blocking IO Non-Blocking IO Request 1 Request 2 Send Responses Pick up requests from queue Node.js Platform internal Thread PoolEvent Queue Send Response to Request n Pick up one Thread from pool Blocking IO
  • 9. Slide 9 www.edureka.co/mastering-node-jsSlide 9 Use-Cases of Node.js (Contd.)  Previously LinkedIn Mobile was powered by Ruby on Rails  It was a synchronous app in which clients used to make several calls for a single page, consequently the system was bursting because of the load
  • 10. Slide 10 www.edureka.co/mastering-node-jsSlide 10 Use-Cases of Node.js (Contd.)  Previously LinkedIn Mobile was powered by Ruby on Rails  It was a synchronous app in which clients used to make several calls for a single page, consequently the system was bursting because of the load Advantage of using Node.js  The LinkedIN moved to Node.js which enabled them to move to a model where the client makes a single request for a page  It led to reduction in the number of machines used to host their services greater than 10:1
  • 11. Slide 11 www.edureka.co/mastering-node-jsSlide 11 Use-Cases of Node.js (Contd.)  Ebay was making real time applications with huge number of eBay- specific services  As eBay was having java infrastructure, it consumed many more resources than expected, raising questions about scalability for production
  • 12. Slide 12 www.edureka.co/mastering-node-jsSlide 12  Ebay was making real time applications with huge number of eBay- specific services  As eBay was having java infrastructure, it consumed many more resources than expected, raising questions about scalability for production  Node.js solved this issue as it is scalable because its single threaded so less overheads and that is why it utilizes the resources in the right manner, so no extra consumption of resources Use-Cases of Node.js (Contd.) Advantage of using Node.js
  • 13. Slide 13 www.edureka.co/mastering-node-jsSlide 13 Uber’s Story  Uber is a transportation company which allows users to get a taxi, private car or rideshare from their smartphones Uber uses NodeJS to implement their cab dispatch operation
  • 14. Slide 14 www.edureka.co/mastering-node-jsSlide 14 Uber - Old Architecture PHP PHP PHP PHP
  • 15. Slide 15 www.edureka.co/mastering-node-jsSlide 15 Uber - Multiple Dispatch Problem  Since PHP is a multithreaded language , each user’s request is handled in a separate thread  Once one car is dispatched for a user, in between the same car get dispatched to another user  Reason was car dispatch operation was executed from multiple threads
  • 16. Slide 16 www.edureka.co/mastering-node-jsSlide 16 Uber - New Architecture iPhone Android NodeJS Dispatch (NodeJS) Dispatch State (MongoDB) Real-time Logic Business Logic Persistent Store (MySQL) Python
  • 17. Slide 17 www.edureka.co/mastering-node-jsSlide 17 Job Trends From the graph below : The number of jobs are skyrocketing.
  • 18. Let us see how communication works in Node.js
  • 19. Slide 19 www.edureka.co/mastering-node-jsSlide 19Slide 19Slide 19  For Network communication on Node.js, we use the “net” module  net.createServer([options][,callback]) :  If allowHalfOpen is true then when the other side initiates Connection Termination the server WILL NOT send the FIN packet Network Communication - TCP //options object { allowHalfOpen : false , pauseOnConnect : false } //values : true or false, default : false
  • 20. Slide 20 www.edureka.co/mastering-node-jsSlide 20Slide 20Slide 20 //Creating a TCP Server var net = require(‘net’); var server = net.createServer(function(socket) { socket.end(‘Hello World’); //Socket is a Duplex stream }); server.listen(3000,function() { console.log(“Server is listening on Port 3000”); }); Network Communication - TCP
  • 21. Slide 21 www.edureka.co/mastering-node-jsSlide 21Slide 21Slide 21 //A TCP client var net = require(‘net’); var socket = net.createConnection({port: 3000,host: ‘192.168.0.1’); socket.on(‘connect’,function() { console.log(‘connected to the server’); }) socket.end(‘Hello Server’); //we can now create a command line TCP chat server and client by using the //process.stdin (Readable Stream) and process.stdout(Writable) Network Communication - TCP
  • 22. Demo
  • 23. Slide 23 www.edureka.co/mastering-node-jsSlide 23Slide 23Slide 23 Two Way Communication : Socket.io  Socket.io is a fast, real-time engine.  Transmitting messages and Receiving message between client and server is simple: Events.  On server/client when sending a message use socket.emit(‘eventname’,data). “eventname” can be any string  And data can be any data: even Binary data!  On server/client when you want to listen to events-messages use socket.on(‘eventname’,callbackFunction).  Where the callbackFunction is a function that accepts a Data argument : Data sent by the other party.
  • 24. Slide 24 www.edureka.co/mastering-node-jsSlide 24Slide 24Slide 24  A simple example: //Server-side var app = require('express')(); var server = require('http').Server(app); var io = require('socket.io')(server); server.listen(80); app.get('/', function (req, res) { res.sendfile(__dirname + '/index.html'); }); io.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); //client-side <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script> Two Way Communication : Socket.io
  • 25. Slide 25 www.edureka.co/mastering-node-jsSlide 25Slide 25Slide 25  Besides ‘connect’, ‘message’ and ‘disconnect’ you can use any custom event names  You could also have a separation of concerns by namespacing. Namespacing also means, that the same websocket connection is used but is multiplexed var io = require('socket.io').listen(80); var chat = io .of('/chat') .on('connection', function (socket) { socket.emit('a message', { that: 'only' , '/chat': 'will get' }); }); var news = io .of('/news') .on('connection', function (socket) { socket.emit('item', { news: 'item' }); }); <script> var chat = io.connect('http://localhost/chat') , news = io.connect('http://localhost/news'); chat.on('connect', function () { chat.emit('hi!'); }); news.on('news', function () { news.emit('woot'); }); </script> Two Way Communication : Socket.io
  • 26. Demo
  • 27. Slide 27Slide 27Slide 27Slide 27 www.edureka.co/mastering-node-js Certifications Get certified in Mastering Node.js by Edureka Edureka's Mastering Node.js course: • You will learn how to develop fast real-time network applications using Node.js, ExpressJS and MongoDB, deal with templating engines like Jade/Hogan/Handlebars and understand testing using Mocha/Jasmine • It will train you to build networking and web based applications that are far more superior and efficient than applications build in other languages. • Get to work on a To-Do List App Project towards the end of the course, which gives you complete insights on the Node.js framework. • Online Live Courses: 27 hours • Assignments: 20 hours • Project: 20 hours • Lifetime Access + 24 X 7 Support Go to www.edureka.co/mastering-node-js
  • 28. Thank You Questions/Queries/Feedback Recording and presentation will be made available to you within 24 hours