SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
In Real Time - The Beginning
Kristian Ačkar
A little about me...
Kristian Ačkar - employed in Core Incubator
● “System Analysis & Traffic. With a sparkle in his eye and
passion for football”
“Core Incubator is an angel investor, providing pre-seed
and seed investment of a small scale. We are also a
startup hub offering coworking space and mentorship,
for startups eager to grow and develop.” - www.coreincubator.com
Contact me:
2
What to expect ahead...
● Introduction
● What can you do with Node.js
● When to use Node.js and when not
● Scalability
● Node.js ecosystem
● Socket.io
● Who uses Node.js
● Zdravko Mamić
3
Background
● Node.js is an open-source cross-platform JavaScript runtime built on
Chrome’s V8 JavaScript engine
● Originally written in 2009 by Ryan Dahl
● Latest stable release v5.5.0
4
Introduction: Basic
● Node.js is “server side JavaScript”
● High-performance network applications framework
● Optimized for high concurrent environments
● ~40% written in JavaScript and ~60% in C++
“As an asynchronous event driven framework, Node.js is designed to build
scalable network applications” - from nodejs.org
5
Introduction: Advanced
● Node.js uses an event-driven, non-blocking I/O model
● It makes use of event-loops via JavaScript’s callback functionality to
implement the non-blocking I/O
● Most of familiar JavaScript from browser is ported to Node.js, except the
DOM/BOM implementation
var x = document.getElementsByTagName(“p”)
var y = window.innerWidth
● Everything inside Node.js runs in a single-thread
6
Introduction: Some (confusing) theory (1)
Event-loops
● Core of event-driven programming (almost all the UI programs use event-
loops to track the user event)
● A major usage of JavaScript is to interact with the DOM (browser) - use of
event-based API was natural
7
File System
Database
Network
...
INTENSIVE
OPERATION
EVENT
QUEUE
Register Callback
Operation Complete
Trigger Callback
EVENT LOOP
(single thread)
Introduction: Some (confusing) theory (2)
Non-blocking I/O
fs.unlinkSync(“/tmp/hello”);
console.log(“I was blocked”); // execution is blocked
fs.unlink(“/tmp/hello”, (err) => {
if (err) throw err;
console.log(“/tmp/hello deleted”);
});
console.log(“I wasn’t blocked”); // execution is not blocked
8
What can you do with Node.js
● All kind of servers
○ HTTP server
○ TCP server Example: TCP client - server
○ DNS server
○ Static file server
● Real-time applications
○ Chat
○ Online games
○ Collaboration tools Example: Drawing collaborating tool
○ Anything which sends updates to the user in real-time
● Desktop GUI applications
● Any kind of applications
9
When to use Node.js and when not
Use Node.js
● For applications where you’d like to maintain persistent connection from the
browser back to the server (“long-polling”)
● When you can reuse a lot of code across the client/server gap (Meteor.js)
● For applications that have a lot of concurrent connections and each request
only needs very few CPU cycles (because the event loop is blocked during
execution of a function)
Don’t use Node.js
● When server request is dependant on heavy CPU consuming algorithm/job
10
Scalability
● Horizontal scalability is problem
● Adding more CPU cores won’t increase performance
● SOLUTION
○ Utilize multi-core CPUs e.g. by setting up cluster (nodejs.org/api/cluster.html)
○ Setup a load balancer and spin up more servers
Example: Node.js clustering
11
Scalability: clusters increase performance
● Apache Benchmark
● 8 core CPU
12
Concurrent Connections 1 2 4 8 16
Single Process 654 711 783 776 754
8 Workers 594 1198 2110 3010 3024
Node.js ecosystem (1)
● Node.js framework is structured in modules
○ Core modules (fs, cluster, http, net, dgram, crypto, ...)
○ Custom modules
const PI = Math.PI;
exports.area = function(r) {
return PI * r * r;
}
exports.circumference = function(r) {
return 2 * r * PI;
}
const circle = require(“./circle.js”);
console.log(“The area of a circle of radius 10 is ${circle.area(10)}”);
13
circle.js
demo.js
Node.js ecosystem (2)
Node Package Manager - NPM (www.npmjs.com)
● Node.js packages repository
● Package is custom made module published in the repository (available to community)
● Real power of Node.js (238 635 packages in repository)
○ Production development heavily depends on Node.js packages
● Countless number of downloads
○ 147 308 788 in the last day
○ 816 096 961 in the last week
○ 3 447 929 034 in the last month
● Install local or global
○ npm install -g sails@0.12.0 vs npm install sails@0.12.0
● Node.js project can define list of dependencies in a package.json file (example)
14
Socket.io
● Node.js (JavaScript) is event driven system
○ Usually we handle events immediately - real-time
■ e.g. file watching example - isn’t that example of “real time“ app?
● Most of the time when we talk about “real-time” and Node.js we mean on one
of the most popular Node.js module - socket.io (http://socket.io)
○ Uses websocket protocol to communicate with a server
○ Simple and lightweight
○ Uses “heartbeats” to control connection health
○ Server can separate socket connections into “rooms”
■ Example apps: chat systems with rooms, brazuca
Example: Drawing collaborating tool
15
Who uses Node.js (in production)
16
● Big shots
○ github.com/nodejs/node-v0.x-archive/wiki/Projects,-Applications,-and-Companies-Using-Node
End of presentation…
...start of discussion 17

Weitere ähnliche Inhalte

Was ist angesagt?

Ansible
AnsibleAnsible
Ansible
gnosek
 
CloudModule for Zabbix
CloudModule for ZabbixCloudModule for Zabbix
CloudModule for Zabbix
Daisuke Ikeda
 
Libcontainer: joining forces under one roof
Libcontainer: joining forces under one roofLibcontainer: joining forces under one roof
Libcontainer: joining forces under one roof
Andrey Vagin
 
(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web
Web::Strategija
 
2013 PyCon SG - Building your cloud infrastructure with Python
2013 PyCon SG - Building your cloud infrastructure with Python2013 PyCon SG - Building your cloud infrastructure with Python
2013 PyCon SG - Building your cloud infrastructure with Python
George Goh
 

Was ist angesagt? (20)

Gluster as Block Store in Containers
Gluster as Block Store in ContainersGluster as Block Store in Containers
Gluster as Block Store in Containers
 
Minio ♥ Go
Minio ♥ GoMinio ♥ Go
Minio ♥ Go
 
Arbiter volumes in gluster
Arbiter volumes in glusterArbiter volumes in gluster
Arbiter volumes in gluster
 
Object Storage in a Cloud-Native Container Envirnoment
Object Storage in a Cloud-Native Container EnvirnomentObject Storage in a Cloud-Native Container Envirnoment
Object Storage in a Cloud-Native Container Envirnoment
 
Integrating GlusterFS with iSCSI Target
Integrating GlusterFS with iSCSI TargetIntegrating GlusterFS with iSCSI Target
Integrating GlusterFS with iSCSI Target
 
High Performance Scaling Techniques in Golang Using Go Assembly
High Performance Scaling Techniques in Golang Using Go AssemblyHigh Performance Scaling Techniques in Golang Using Go Assembly
High Performance Scaling Techniques in Golang Using Go Assembly
 
Ansible
AnsibleAnsible
Ansible
 
Monitoring your shiny new docker environment
Monitoring your shiny new docker environmentMonitoring your shiny new docker environment
Monitoring your shiny new docker environment
 
Everyone Loves a Sausage
Everyone Loves a SausageEveryone Loves a Sausage
Everyone Loves a Sausage
 
Cloud foundry on kubernetes
Cloud foundry on kubernetesCloud foundry on kubernetes
Cloud foundry on kubernetes
 
Game DDOS Prevention
Game DDOS PreventionGame DDOS Prevention
Game DDOS Prevention
 
Cassandra 2.1 boot camp, exercise
Cassandra 2.1 boot camp, exerciseCassandra 2.1 boot camp, exercise
Cassandra 2.1 boot camp, exercise
 
CloudModule for Zabbix
CloudModule for ZabbixCloudModule for Zabbix
CloudModule for Zabbix
 
OSBConf 2015 | Scale out backups with bareos and gluster by niels de vos
OSBConf 2015 | Scale out backups with bareos and gluster by niels de vosOSBConf 2015 | Scale out backups with bareos and gluster by niels de vos
OSBConf 2015 | Scale out backups with bareos and gluster by niels de vos
 
How to access your FIWARE Lab Instance.
How to access your FIWARE Lab Instance.How to access your FIWARE Lab Instance.
How to access your FIWARE Lab Instance.
 
Osis18_Cloud : DeepTorrent Stockage distribué perenne basé sur Bittorrent
Osis18_Cloud : DeepTorrent Stockage distribué perenne basé sur BittorrentOsis18_Cloud : DeepTorrent Stockage distribué perenne basé sur Bittorrent
Osis18_Cloud : DeepTorrent Stockage distribué perenne basé sur Bittorrent
 
Libcontainer: joining forces under one roof
Libcontainer: joining forces under one roofLibcontainer: joining forces under one roof
Libcontainer: joining forces under one roof
 
(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web(WS14) Sasa Matijasic - Node.js i "novi" web
(WS14) Sasa Matijasic - Node.js i "novi" web
 
Node js beginner
Node js beginnerNode js beginner
Node js beginner
 
2013 PyCon SG - Building your cloud infrastructure with Python
2013 PyCon SG - Building your cloud infrastructure with Python2013 PyCon SG - Building your cloud infrastructure with Python
2013 PyCon SG - Building your cloud infrastructure with Python
 

Ähnlich wie Node in Real Time - The Beginning

Ähnlich wie Node in Real Time - The Beginning (20)

Nodejs
NodejsNodejs
Nodejs
 
New Jersey Red Hat Users Group Presentation: Provisioning anywhere
New Jersey Red Hat Users Group Presentation: Provisioning anywhereNew Jersey Red Hat Users Group Presentation: Provisioning anywhere
New Jersey Red Hat Users Group Presentation: Provisioning anywhere
 
18_Node.js.ppt
18_Node.js.ppt18_Node.js.ppt
18_Node.js.ppt
 
18_Node.js.ppt
18_Node.js.ppt18_Node.js.ppt
18_Node.js.ppt
 
Netty training
Netty trainingNetty training
Netty training
 
Nodejs
NodejsNodejs
Nodejs
 
Netty training
Netty trainingNetty training
Netty training
 
Treinamento frontend
Treinamento frontendTreinamento frontend
Treinamento frontend
 
Introduction to node.js aka NodeJS
Introduction to node.js aka NodeJSIntroduction to node.js aka NodeJS
Introduction to node.js aka NodeJS
 
Swarm: Native Docker Clustering
Swarm: Native Docker ClusteringSwarm: Native Docker Clustering
Swarm: Native Docker Clustering
 
Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6Tech io nodejs_20130531_v0.6
Tech io nodejs_20130531_v0.6
 
Node.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scaleNode.js Web Apps @ ebay scale
Node.js Web Apps @ ebay scale
 
Introduction to node.js By Ahmed Assaf
Introduction to node.js  By Ahmed AssafIntroduction to node.js  By Ahmed Assaf
Introduction to node.js By Ahmed Assaf
 
2012-03-15 What's New at Red Hat
2012-03-15 What's New at Red Hat2012-03-15 What's New at Red Hat
2012-03-15 What's New at Red Hat
 
Heterogeneous multiprocessing on androd and i.mx7
Heterogeneous multiprocessing on androd and i.mx7Heterogeneous multiprocessing on androd and i.mx7
Heterogeneous multiprocessing on androd and i.mx7
 
Guest Agents: Support & Implementation
Guest Agents: Support & ImplementationGuest Agents: Support & Implementation
Guest Agents: Support & Implementation
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Kubernetes: My BFF
Kubernetes: My BFFKubernetes: My BFF
Kubernetes: My BFF
 
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other ThingsI Just Want to Run My Code: Waypoint, Nomad, and Other Things
I Just Want to Run My Code: Waypoint, Nomad, and Other Things
 
NetflixOSS Meetup season 3 episode 1
NetflixOSS Meetup season 3 episode 1NetflixOSS Meetup season 3 episode 1
NetflixOSS Meetup season 3 episode 1
 

Mehr von Axilis

Mehr von Axilis (20)

Web App Security for Devs
Web App Security for DevsWeb App Security for Devs
Web App Security for Devs
 
React tips
React tipsReact tips
React tips
 
Configuring SSL on NGNINX and less tricky servers
Configuring SSL on NGNINX and less tricky serversConfiguring SSL on NGNINX and less tricky servers
Configuring SSL on NGNINX and less tricky servers
 
Journey to Microservice architecture via Amazon Lambda
Journey to Microservice architecture via Amazon LambdaJourney to Microservice architecture via Amazon Lambda
Journey to Microservice architecture via Amazon Lambda
 
Should you react?
Should you react?Should you react?
Should you react?
 
Sweet ES2015 (ES6) Taste
Sweet ES2015 (ES6) TasteSweet ES2015 (ES6) Taste
Sweet ES2015 (ES6) Taste
 
NuGet Must Haves for LINQ
NuGet Must Haves for LINQNuGet Must Haves for LINQ
NuGet Must Haves for LINQ
 
Quick introduction to zeplin
Quick introduction to zeplinQuick introduction to zeplin
Quick introduction to zeplin
 
Diving into Node with Express and Mongo
Diving into Node with Express and MongoDiving into Node with Express and Mongo
Diving into Node with Express and Mongo
 
Road to Dynamic LINQ - Part 2
 Road to Dynamic LINQ - Part 2 Road to Dynamic LINQ - Part 2
Road to Dynamic LINQ - Part 2
 
Road to Dynamic LINQ Part 1
Road to Dynamic LINQ Part 1Road to Dynamic LINQ Part 1
Road to Dynamic LINQ Part 1
 
.NET Core - Sve što trebate znati
.NET Core - Sve što trebate znati.NET Core - Sve što trebate znati
.NET Core - Sve što trebate znati
 
Angular Translate
Angular TranslateAngular Translate
Angular Translate
 
NPM, Bower and Gulp Kickstart in Visual Studio
 NPM, Bower and Gulp Kickstart in Visual Studio NPM, Bower and Gulp Kickstart in Visual Studio
NPM, Bower and Gulp Kickstart in Visual Studio
 
Dive Into Swift
Dive Into SwiftDive Into Swift
Dive Into Swift
 
Python Tools for Visual Studio
Python Tools for Visual StudioPython Tools for Visual Studio
Python Tools for Visual Studio
 
Python Tools for Visual Studio
Python Tools for Visual StudioPython Tools for Visual Studio
Python Tools for Visual Studio
 
Wireframing
WireframingWireframing
Wireframing
 
Angular 2.0: Getting ready
Angular 2.0: Getting readyAngular 2.0: Getting ready
Angular 2.0: Getting ready
 
Angular JS deep dive
Angular JS deep diveAngular JS deep dive
Angular JS deep dive
 

Kürzlich hochgeladen

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Kürzlich hochgeladen (20)

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 

Node in Real Time - The Beginning

  • 1. In Real Time - The Beginning Kristian Ačkar
  • 2. A little about me... Kristian Ačkar - employed in Core Incubator ● “System Analysis & Traffic. With a sparkle in his eye and passion for football” “Core Incubator is an angel investor, providing pre-seed and seed investment of a small scale. We are also a startup hub offering coworking space and mentorship, for startups eager to grow and develop.” - www.coreincubator.com Contact me: 2
  • 3. What to expect ahead... ● Introduction ● What can you do with Node.js ● When to use Node.js and when not ● Scalability ● Node.js ecosystem ● Socket.io ● Who uses Node.js ● Zdravko Mamić 3
  • 4. Background ● Node.js is an open-source cross-platform JavaScript runtime built on Chrome’s V8 JavaScript engine ● Originally written in 2009 by Ryan Dahl ● Latest stable release v5.5.0 4
  • 5. Introduction: Basic ● Node.js is “server side JavaScript” ● High-performance network applications framework ● Optimized for high concurrent environments ● ~40% written in JavaScript and ~60% in C++ “As an asynchronous event driven framework, Node.js is designed to build scalable network applications” - from nodejs.org 5
  • 6. Introduction: Advanced ● Node.js uses an event-driven, non-blocking I/O model ● It makes use of event-loops via JavaScript’s callback functionality to implement the non-blocking I/O ● Most of familiar JavaScript from browser is ported to Node.js, except the DOM/BOM implementation var x = document.getElementsByTagName(“p”) var y = window.innerWidth ● Everything inside Node.js runs in a single-thread 6
  • 7. Introduction: Some (confusing) theory (1) Event-loops ● Core of event-driven programming (almost all the UI programs use event- loops to track the user event) ● A major usage of JavaScript is to interact with the DOM (browser) - use of event-based API was natural 7 File System Database Network ... INTENSIVE OPERATION EVENT QUEUE Register Callback Operation Complete Trigger Callback EVENT LOOP (single thread)
  • 8. Introduction: Some (confusing) theory (2) Non-blocking I/O fs.unlinkSync(“/tmp/hello”); console.log(“I was blocked”); // execution is blocked fs.unlink(“/tmp/hello”, (err) => { if (err) throw err; console.log(“/tmp/hello deleted”); }); console.log(“I wasn’t blocked”); // execution is not blocked 8
  • 9. What can you do with Node.js ● All kind of servers ○ HTTP server ○ TCP server Example: TCP client - server ○ DNS server ○ Static file server ● Real-time applications ○ Chat ○ Online games ○ Collaboration tools Example: Drawing collaborating tool ○ Anything which sends updates to the user in real-time ● Desktop GUI applications ● Any kind of applications 9
  • 10. When to use Node.js and when not Use Node.js ● For applications where you’d like to maintain persistent connection from the browser back to the server (“long-polling”) ● When you can reuse a lot of code across the client/server gap (Meteor.js) ● For applications that have a lot of concurrent connections and each request only needs very few CPU cycles (because the event loop is blocked during execution of a function) Don’t use Node.js ● When server request is dependant on heavy CPU consuming algorithm/job 10
  • 11. Scalability ● Horizontal scalability is problem ● Adding more CPU cores won’t increase performance ● SOLUTION ○ Utilize multi-core CPUs e.g. by setting up cluster (nodejs.org/api/cluster.html) ○ Setup a load balancer and spin up more servers Example: Node.js clustering 11
  • 12. Scalability: clusters increase performance ● Apache Benchmark ● 8 core CPU 12 Concurrent Connections 1 2 4 8 16 Single Process 654 711 783 776 754 8 Workers 594 1198 2110 3010 3024
  • 13. Node.js ecosystem (1) ● Node.js framework is structured in modules ○ Core modules (fs, cluster, http, net, dgram, crypto, ...) ○ Custom modules const PI = Math.PI; exports.area = function(r) { return PI * r * r; } exports.circumference = function(r) { return 2 * r * PI; } const circle = require(“./circle.js”); console.log(“The area of a circle of radius 10 is ${circle.area(10)}”); 13 circle.js demo.js
  • 14. Node.js ecosystem (2) Node Package Manager - NPM (www.npmjs.com) ● Node.js packages repository ● Package is custom made module published in the repository (available to community) ● Real power of Node.js (238 635 packages in repository) ○ Production development heavily depends on Node.js packages ● Countless number of downloads ○ 147 308 788 in the last day ○ 816 096 961 in the last week ○ 3 447 929 034 in the last month ● Install local or global ○ npm install -g sails@0.12.0 vs npm install sails@0.12.0 ● Node.js project can define list of dependencies in a package.json file (example) 14
  • 15. Socket.io ● Node.js (JavaScript) is event driven system ○ Usually we handle events immediately - real-time ■ e.g. file watching example - isn’t that example of “real time“ app? ● Most of the time when we talk about “real-time” and Node.js we mean on one of the most popular Node.js module - socket.io (http://socket.io) ○ Uses websocket protocol to communicate with a server ○ Simple and lightweight ○ Uses “heartbeats” to control connection health ○ Server can separate socket connections into “rooms” ■ Example apps: chat systems with rooms, brazuca Example: Drawing collaborating tool 15
  • 16. Who uses Node.js (in production) 16 ● Big shots ○ github.com/nodejs/node-v0.x-archive/wiki/Projects,-Applications,-and-Companies-Using-Node