Kristian Ačkar from Core Incubator visits us to showcase strength of Node.js for real time applications. If you're interested in this topic come and join this lecture in real time at Axilis.
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
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