SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
Emerging Paradigms - 
Server Side Event Driven 
Programming 
Kamal Hussain 
http://www.linkedin.com/in/hussainkamal/
Agenda 
● Linear Vs Nonlinear Code 
● Concurrency through threads and events 
● Event loop 
● PHP and Javascript 
● Node.js - closeup 
● Important concepts
We are used to .. 
val client = new HttpClient() 
val method = new GetMethod("http: 
//www.google.com") 
val statusCode = client.executeMethod 
(method) 
println("Server responded with %d" . 
format(statusCode))
Event driven approach 
var callback = function(data) { 
console.log("firing callback " + 
data); 
}; 
$.get('/endpoint', callback); 
console.log('Did you see callback?');
Achieving scale and concurrency 
● Multiple threads/processes 
● Size the threadpool correctly 
● Each thread is responsible for one task such 
as serving a request 
● Asynchronous programming
Asynchronous with 
threads/processes
Asynchronous with events
Problems with Threads 
● Hard to program 
● Memory requirements are high 
● Large overhead 
● Context switching 
● Priority inversion
Threads wait 
udemy.com
Cost of IO 
L1 Cache 3 Cycles 
L2 Cache 14 Cycles 
RAM 250 cycles 
Disk 41 000 000 cycles 
Network 240 000 000 cycles
Event Loop Architecture 
courtsey: http://www.johanndutoit.net/
Writing synchronous Vs 
Asynchronous 
// Good: write files asynchronously 
fs.writeFile('message.txt', 'Hello Node', function (err) { 
console.log("It's saved and the server remains responsive!"); 
}); 
// BAD: write files synchronously 
fs.writeFileSync('message.txt', 'Hello Node'); 
console.log("It's saved, but you just blocked ALL requests!"); 
This can cause performance drop from thousands of requests/seconds to a few 
dozen/second. 
http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-10-performance-tips-linkedin- 
mobile
Tale of two languages 
● PHP was invented in 1994 by Rasmus 
Lerdorfas as a replacement for CGI scripts 
● PHP was a substitute for single-threaded C 
programs 
● Brendan Eich developed Javascript in 1995 
for a completely different purpose. JS was 
designed to run within Netscape Navigator 
and was primarily designed to handle 
events. 
PHP -> eventless, Javascript -> eventful
Node.js 
● Ryan Dahl invented Node.js in 2009 as a 
continuation of Javascript heritage. 
● Node.js is modeled after multi-task, multi-page 
handling of a web server.
What's Node.js? 
"Node.js is a server-side software system designed for 
writing scalable Internet applications, notably web servers. 
Programs are written on the server side in JavaScript, 
using event-driven, asynchronous I/O to minimize overhead 
and maximize scalability." 
- from wikipedia
Node.js - asynchronous I/O 
● First class functions 
● Closures 
● Event loop 
● Callback counters 
CPU intensive tasks are delegated to workers
PHP Way 
$fp = fopen("fp.txt", 'w) 
fwrite($fp, "hello world"); 
fclose($fp);
Node.js way 
var fs = require('fs'); 
fs.open('fp.txt', 'w', 0666, 
function(error, fp) { 
fw.write(fp, 'helloworld', null, 
'utf-8', function() { 
fs.close(fp, function(error) { 
}); 
}); 
});
Node.js simple example 
var http = require('http'); 
http.createServer(function(req, res) { 
res.writeHead(200, {'Content-Type' : 
'text/plain'}); 
res.end("Hello Worldn"); 
}).listen(8000, "127.0.0.1"); 
console.log("Server running at http: 
//127.0.0.1:8000");
Node.js Core APIs 
Events 
EventTransmitter 
Event listener 
Event emitter 
Call back 
Http 
I/O
Node.js Workers 
Synchronous call 
Workers are blocked 
Call returns
Workers Vs Events 
Workers 
1 event per connection 
N workers per CPU 
Events 
N connections per CPU 
1 process per CPU
Node.js Typical Applications 
● Proxy 
● API server 
○ REST API calls 
○ Simple transformations 
See performance comparisons at: 
http://www.slideshare.net/FabianFrankDe/nodejs-performance-case-study
Concepts to learn 
First class functions 
Lambdas - anonymous functions 
Closures 
Non-blocking IO
Deploying Node.js 
http://www.slideshare.net/BenLin2/webconf-nodejsproductionarchitecture
Key takeaway 
Learn Javascript and 
functional programming. 
Future is brighter :)
Reference 
● Node - Up and Running by Tom Hughes-Croucher, 
Mike Wilson - Oreilly 
● Node.js for PHP Developers - Oreilly 
● Javascript: The definitive guide - Oreilly 
● LinkedIn, Netflix Blogs 
● http://architects.dzone.com/articles/nodejs-php-programmers- 
1-event 
● https://www.udemy.com/lectures/understanding-the-nodejs- 
event-loop-91298 
● https://speakerdeck.com/guldenpt/before-start-coding-in- 
node-dot-js

Más contenido relacionado

La actualidad más candente

Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012 Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Tom Croucher
 
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Using Node.js to  Build Great  Streaming Services - HTML5 Dev ConfUsing Node.js to  Build Great  Streaming Services - HTML5 Dev Conf
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Tom Croucher
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHP
Thomas Weinert
 

La actualidad más candente (20)

Let’s talk about JavaScript - WebElement
Let’s talk about JavaScript - WebElementLet’s talk about JavaScript - WebElement
Let’s talk about JavaScript - WebElement
 
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
Beautiful code instead of callback hell using ES6 Generators, Koa, Bluebird (...
 
Everything ruby
Everything rubyEverything ruby
Everything ruby
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
 
Create Rest API in Nodejs
Create Rest API in Nodejs Create Rest API in Nodejs
Create Rest API in Nodejs
 
Node.js concurrency
Node.js concurrencyNode.js concurrency
Node.js concurrency
 
Event Driven Architecture Concepts in Web Technologies - Part 1
Event Driven Architecture Concepts in Web Technologies - Part 1Event Driven Architecture Concepts in Web Technologies - Part 1
Event Driven Architecture Concepts in Web Technologies - Part 1
 
Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012 Streams are Awesome - (Node.js) TimesOpen Sep 2012
Streams are Awesome - (Node.js) TimesOpen Sep 2012
 
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
Using Node.js to  Build Great  Streaming Services - HTML5 Dev ConfUsing Node.js to  Build Great  Streaming Services - HTML5 Dev Conf
Using Node.js to Build Great Streaming Services - HTML5 Dev Conf
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
React PHP: the NodeJS challenger
React PHP: the NodeJS challengerReact PHP: the NodeJS challenger
React PHP: the NodeJS challenger
 
Javascript Everywhere From Nose To Tail
Javascript Everywhere From Nose To TailJavascript Everywhere From Nose To Tail
Javascript Everywhere From Nose To Tail
 
Asynchronous I/O in PHP
Asynchronous I/O in PHPAsynchronous I/O in PHP
Asynchronous I/O in PHP
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
 
Introduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCatsIntroduction to NodeJS with LOLCats
Introduction to NodeJS with LOLCats
 
20170310 PHP goal pyramid for memorising
20170310 PHP goal pyramid for memorising20170310 PHP goal pyramid for memorising
20170310 PHP goal pyramid for memorising
 
Securing Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp VaultSecuring Prometheus exporters using HashiCorp Vault
Securing Prometheus exporters using HashiCorp Vault
 
Shell Scripting & Ruby Hacking
Shell Scripting & Ruby HackingShell Scripting & Ruby Hacking
Shell Scripting & Ruby Hacking
 
Node.js
Node.jsNode.js
Node.js
 
Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...
Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...
Shell Scripting-training-course-navi-mumbai-shell-scripting-course-provider-n...
 

Destacado (18)

Contentspagefeedback
ContentspagefeedbackContentspagefeedback
Contentspagefeedback
 
Silabus Eksponensial dan Logaritma
Silabus Eksponensial dan LogaritmaSilabus Eksponensial dan Logaritma
Silabus Eksponensial dan Logaritma
 
analisis
analisisanalisis
analisis
 
Media reader profile
Media reader profileMedia reader profile
Media reader profile
 
Senza nome 1
Senza nome 1Senza nome 1
Senza nome 1
 
Materi ajar eksponensial
Materi ajar eksponensialMateri ajar eksponensial
Materi ajar eksponensial
 
A Way Back to You
A Way Back to YouA Way Back to You
A Way Back to You
 
Hyderabad traithlon presentation
Hyderabad traithlon presentationHyderabad traithlon presentation
Hyderabad traithlon presentation
 
Rpp eksponen.tugas tik dlm tp
Rpp eksponen.tugas tik dlm tpRpp eksponen.tugas tik dlm tp
Rpp eksponen.tugas tik dlm tp
 
Journal-RISS2015
Journal-RISS2015Journal-RISS2015
Journal-RISS2015
 
Latihan soal
Latihan soalLatihan soal
Latihan soal
 
Materi ajar eksponensial
Materi ajar eksponensialMateri ajar eksponensial
Materi ajar eksponensial
 
Silabus eksponen
Silabus eksponenSilabus eksponen
Silabus eksponen
 
Bab 1 bentuk pangkat, akar & logaritma
Bab 1 bentuk pangkat, akar & logaritmaBab 1 bentuk pangkat, akar & logaritma
Bab 1 bentuk pangkat, akar & logaritma
 
Materi blog
Materi blogMateri blog
Materi blog
 
Bab 1 pertumbuhan dan perkembangan tumbuhan
Bab 1 pertumbuhan dan perkembangan tumbuhanBab 1 pertumbuhan dan perkembangan tumbuhan
Bab 1 pertumbuhan dan perkembangan tumbuhan
 
Rpp blog
Rpp blogRpp blog
Rpp blog
 
Silabus biologi pratiwi jilid 3
Silabus biologi pratiwi jilid 3Silabus biologi pratiwi jilid 3
Silabus biologi pratiwi jilid 3
 

Similar a Event driven programming -- Node.JS

Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
Tom Croucher
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
Alex Su
 
Node js
Node jsNode js
Node js
hazzaz
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
Sagiv Ofek
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
Mohammad Qureshi
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
WalaSidhom1
 

Similar a Event driven programming -- Node.JS (20)

Server Side Event Driven Programming
Server Side Event Driven ProgrammingServer Side Event Driven Programming
Server Side Event Driven Programming
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
NodeJS
NodeJSNodeJS
NodeJS
 
Node.js
Node.jsNode.js
Node.js
 
Introduction to Node.js Platform
Introduction to Node.js PlatformIntroduction to Node.js Platform
Introduction to Node.js Platform
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
 
Node.js Workshop - Sela SDP 2015
Node.js Workshop  - Sela SDP 2015Node.js Workshop  - Sela SDP 2015
Node.js Workshop - Sela SDP 2015
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Node js
Node jsNode js
Node js
 
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
 
A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...A language for the Internet: Why JavaScript and Node.js is right for Internet...
A language for the Internet: Why JavaScript and Node.js is right for Internet...
 
Intro to Node
Intro to NodeIntro to Node
Intro to Node
 
Node.js for Rubists
Node.js for RubistsNode.js for Rubists
Node.js for Rubists
 
Intro to node and mongodb 1
Intro to node and mongodb   1Intro to node and mongodb   1
Intro to node and mongodb 1
 
nodejs_at_a_glance.ppt
nodejs_at_a_glance.pptnodejs_at_a_glance.ppt
nodejs_at_a_glance.ppt
 
NodeJS
NodeJSNodeJS
NodeJS
 
Node.js 1, 2, 3
Node.js 1, 2, 3Node.js 1, 2, 3
Node.js 1, 2, 3
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
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
 

Último

Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Sheetaleventcompany
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
soniya singh
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
Diya Sharma
 

Último (20)

VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
VVVIP Call Girls In Connaught Place ➡️ Delhi ➡️ 9999965857 🚀 No Advance 24HRS...
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
Ganeshkhind ! Call Girls Pune - 450+ Call Girl Cash Payment 8005736733 Neha T...
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
Call Girls Service Chandigarh Lucky ❤️ 7710465962 Independent Call Girls In C...
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
Russian Call Girls in %(+971524965298  )#  Call Girls in DubaiRussian Call Girls in %(+971524965298  )#  Call Girls in Dubai
Russian Call Girls in %(+971524965298 )# Call Girls in Dubai
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Sarai Rohilla Escort Service Delhi N.C.R.
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...Top Rated  Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
Top Rated Pune Call Girls Daund ⟟ 6297143586 ⟟ Call Me For Genuine Sex Servi...
 
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
₹5.5k {Cash Payment}New Friends Colony Call Girls In [Delhi NIHARIKA] 🔝|97111...
 
Al Barsha Night Partner +0567686026 Call Girls Dubai
Al Barsha Night Partner +0567686026 Call Girls  DubaiAl Barsha Night Partner +0567686026 Call Girls  Dubai
Al Barsha Night Partner +0567686026 Call Girls Dubai
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
 

Event driven programming -- Node.JS

  • 1. Emerging Paradigms - Server Side Event Driven Programming Kamal Hussain http://www.linkedin.com/in/hussainkamal/
  • 2. Agenda ● Linear Vs Nonlinear Code ● Concurrency through threads and events ● Event loop ● PHP and Javascript ● Node.js - closeup ● Important concepts
  • 3. We are used to .. val client = new HttpClient() val method = new GetMethod("http: //www.google.com") val statusCode = client.executeMethod (method) println("Server responded with %d" . format(statusCode))
  • 4. Event driven approach var callback = function(data) { console.log("firing callback " + data); }; $.get('/endpoint', callback); console.log('Did you see callback?');
  • 5. Achieving scale and concurrency ● Multiple threads/processes ● Size the threadpool correctly ● Each thread is responsible for one task such as serving a request ● Asynchronous programming
  • 8. Problems with Threads ● Hard to program ● Memory requirements are high ● Large overhead ● Context switching ● Priority inversion
  • 10. Cost of IO L1 Cache 3 Cycles L2 Cache 14 Cycles RAM 250 cycles Disk 41 000 000 cycles Network 240 000 000 cycles
  • 11.
  • 12. Event Loop Architecture courtsey: http://www.johanndutoit.net/
  • 13. Writing synchronous Vs Asynchronous // Good: write files asynchronously fs.writeFile('message.txt', 'Hello Node', function (err) { console.log("It's saved and the server remains responsive!"); }); // BAD: write files synchronously fs.writeFileSync('message.txt', 'Hello Node'); console.log("It's saved, but you just blocked ALL requests!"); This can cause performance drop from thousands of requests/seconds to a few dozen/second. http://engineering.linkedin.com/nodejs/blazing-fast-nodejs-10-performance-tips-linkedin- mobile
  • 14. Tale of two languages ● PHP was invented in 1994 by Rasmus Lerdorfas as a replacement for CGI scripts ● PHP was a substitute for single-threaded C programs ● Brendan Eich developed Javascript in 1995 for a completely different purpose. JS was designed to run within Netscape Navigator and was primarily designed to handle events. PHP -> eventless, Javascript -> eventful
  • 15. Node.js ● Ryan Dahl invented Node.js in 2009 as a continuation of Javascript heritage. ● Node.js is modeled after multi-task, multi-page handling of a web server.
  • 16. What's Node.js? "Node.js is a server-side software system designed for writing scalable Internet applications, notably web servers. Programs are written on the server side in JavaScript, using event-driven, asynchronous I/O to minimize overhead and maximize scalability." - from wikipedia
  • 17. Node.js - asynchronous I/O ● First class functions ● Closures ● Event loop ● Callback counters CPU intensive tasks are delegated to workers
  • 18. PHP Way $fp = fopen("fp.txt", 'w) fwrite($fp, "hello world"); fclose($fp);
  • 19. Node.js way var fs = require('fs'); fs.open('fp.txt', 'w', 0666, function(error, fp) { fw.write(fp, 'helloworld', null, 'utf-8', function() { fs.close(fp, function(error) { }); }); });
  • 20. Node.js simple example var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, {'Content-Type' : 'text/plain'}); res.end("Hello Worldn"); }).listen(8000, "127.0.0.1"); console.log("Server running at http: //127.0.0.1:8000");
  • 21. Node.js Core APIs Events EventTransmitter Event listener Event emitter Call back Http I/O
  • 22. Node.js Workers Synchronous call Workers are blocked Call returns
  • 23. Workers Vs Events Workers 1 event per connection N workers per CPU Events N connections per CPU 1 process per CPU
  • 24. Node.js Typical Applications ● Proxy ● API server ○ REST API calls ○ Simple transformations See performance comparisons at: http://www.slideshare.net/FabianFrankDe/nodejs-performance-case-study
  • 25. Concepts to learn First class functions Lambdas - anonymous functions Closures Non-blocking IO
  • 27. Key takeaway Learn Javascript and functional programming. Future is brighter :)
  • 28. Reference ● Node - Up and Running by Tom Hughes-Croucher, Mike Wilson - Oreilly ● Node.js for PHP Developers - Oreilly ● Javascript: The definitive guide - Oreilly ● LinkedIn, Netflix Blogs ● http://architects.dzone.com/articles/nodejs-php-programmers- 1-event ● https://www.udemy.com/lectures/understanding-the-nodejs- event-loop-91298 ● https://speakerdeck.com/guldenpt/before-start-coding-in- node-dot-js