No Dej S Presentation
No Dej S Presentation
No Dej S Presentation
introduction
What is Node.js anyway?
a complete software platform for scalable server-side
and networking applications
open-source under the MIT license
comes bundled with a JavaScript interpreter
runs on Linux, Windows, Mac OS & most other
major operating systems
timeline
2009
2010
2011
2012
...
2014
huge success
why?
Up until recently, the web was a stateless environment.
Interactive features were encapsulated within Flash or
Java Applets
Node establishes real-time, two-way connections!
how it works
Built on Chrome's V8 JavaScript runtime for easily
building fast, scalable network applications
Uses an event-driven, non-blocking I/O model that
makes it lightweight and ecient, perfect for dataintensive real-time applications that run across
distributed devices
overall structure
Two major components:
Main core, written in C and C++
Modules, such as Libuv library and V8 runtime engine, also written
in C++
overall structure
Main Single Thread
API
API in JavaScript
Node bindings allow for server
operations
Node Bindings
(socket, http, etc)
V8
Asynchronous
I/O
(libuv)
Event
Loop
(libuv)
DNS
(c-ares)
Crypto
(OpenSSL)
v8 runtime engine
Just in Time compiler, written in C++
Consists of compiler, optimizer, and garbage
collector
libuv
Responsible for Nodes asynchronous I/O operations
Contains fixed-size thread pool
major influences
Heavily influenced by architecture of Unix operating
system
Relies on a small core and layers of libraries and other
modules to facilitate I/O operations
major influences
Built-in package manager contributes
to the modularity of Node
major features
1. Single threaded
major features
2. Event Loop
Automatically terminated
major features
3. Non-blocking I/O
how it works
A simple example: accessing data from a database
1. HTTP
Request
Mobile Client
Node.js
Single thread
3. Data
Database
Web Server
4. Response in
JSON format
via callback
2. Async
Data Query
* Here Node.js, acknowledges the request right away before writing any data to the database.
how it works
A generic model of Node.js
NodeJS
Main Event Loop
Initialize
Run V8
Event
Loop
Exit
Stop
Stop
Thread n
Create Thread
Delegate Task
Idle
Perform Task
I/O Operation
Exit
how it works
A closer look at the Event Loop
Thread 1
Thread 2
Thread n
(function,
callback)
Task 1
Task 2
Node.js
Application
Event Loop
(Libuv)
Callback 1
Task 3
Return 1
Task 4
Distributed
Hierarchical
Data Flow
Implicit Asynchronous
critical analysis
Benefits:
Due to its real-time nature, its possible to process files while they are
being uploaded
critical analysis
Best suited for:
Backend for single-page web apps with same language for client
and server
Quick prototyping
Chat applications
critical analysis
Limitations:
critical analysis
Not suited for:
CPU-bound tasks