21.02.2017
Fatih Şimşek - Software Infrastructure
NodeJs
Open Source, cross platform
Huge community
Ryan Dahl, Joyent
Developed in 2009
Published in 2011 ( Linux + Windows )
Similar design to systems like Ruby's Event Machine or
Python's Twisted
Who is using NodeJs?
Success Stories
Yahoo: Node services handle around 1.680.000 – 2.000.000
requests/minute. Biggest Win: speed & ease of developement
Netflix: The team decided to use Node.js to achieve lightweight,
modular and fast application. As a result, the startup time of
their new app has been reduced by 70%
Linkedin: When compared with the previous Ruby on Rails
based version, the new mobile app is up to 20 times faster
Walmart: %55 of all traffic on Black Friday went to Node
servers. Not a single Node server went down
Success Stories
PayPal: Build a Node version of a Java app in half the time with
fewer developers. Node version of the app doubled the number
of requests/second. Request time dropped 35%
Groupon: In comparison with earlier Ruby on Rails platform,
web pages got faster by about 50% and are able to serve much
higher traffic
GoDaddy: 10x fewer servers to host our customer websites and
we reduced the Time To First Byte (TTFB) considerably from
~60ms to something around ~12ms
Github
NodeJs
Built on Chrome’s Javascript Runtime (V8)
Single thread, event oriented
Event-Driven with Non-blocking I/O
Use a Event Loop for Non-blocking I/O
Can handle thousands of concurrent connections with minimal
overhead (CPU/Memory)
40% JS and 60% C++
Traditional Server
New thread per request
The thread is blocked
during IO operations
Thesis
IO is expensive
Thread per connection
is memory expensive
Architecture
Client
Event loop
(main thread)
C++
Threadpool
(worker
threads)
Clients send HTTP requests
to Node.js server
An Event-loop is woken up by OS,
passes request and response objects
to the thread-pool
Long-running jobs run
on worker threads
Response is sent
back to main thread
via callback
Event loop returns
result to client
Architecture
API in Javascript
Relies on Google’s V8
runtime engine
Node Bindings allow for
server operations (IO,
network)
Libuv responsible for both
asynchronous I/O & event
loop
When to use NodeJs?
Highly Event driven & Heavily I/O bound
Chat application
Online game
Collaboration tool
Monitoring Dashboard
Trader’s Dashboard
When not to use NodeJs?
Heavy CPU intensive calculations on server-side
Concurrent Task based applications
Install
https://nodejs.org/en/download/
node –v (Node version)
Developement Environment
Web Storm
Sublime Text
Atom
Nodeclipse
Visual Studio
Visual Studio Code
DEMO
Node Frameworks & Tools
NPM (Package Manager)
Gulp / Grunt
Express.js
Frameworks built on Express.js (LoopBack / MEAN / Sails)
Restify
Mongoose
Socket.io
NPM
Package manager for Node
Bundled and installed automatically with the environment
Frequently Usage
• npm install --save package_name
• npm update
How Does it works?
• Read package.json
• Installs the dependencies in the local node_modules folder
• In global modes, it make a node module accessible to all
Express.js
Web application framework
Minimal and Flexible
Many Framework based on Express
Features
• MVC
• Routing
• Session Support
• Cookie Parsing
• Middleware (Request-Response Cycle)
• View Engines (Jade, Ejs, Vash)
Vash
Familiar Razor engine experience
No special template syntax to learn
Syntax:
Mongoose
Object Data Modelling for Node.js
Officially supported by 10gen,Inc
Features:
• Async and sync validation of models
• Model casting
• Object lifecycle management (hook save / validate)
• Query Builder
• Schema Deriven Design
DEMO
EventEmitter
Most of built-in Node libraries use it
Use Observer Pattern
Socket.io
Realtime application framework
Cross browser (Web socket + fallback)
Bi-directional communication
Automatic compression
IOS, Android, NativeScript support
Two parts
• Client side (run on browser)
• Server side (run on Node)
DEMO
Benchmark
Benchmark
Node js

Node js

  • 1.
    21.02.2017 Fatih Şimşek -Software Infrastructure
  • 2.
    NodeJs Open Source, crossplatform Huge community Ryan Dahl, Joyent Developed in 2009 Published in 2011 ( Linux + Windows ) Similar design to systems like Ruby's Event Machine or Python's Twisted
  • 3.
    Who is usingNodeJs?
  • 4.
    Success Stories Yahoo: Nodeservices handle around 1.680.000 – 2.000.000 requests/minute. Biggest Win: speed & ease of developement Netflix: The team decided to use Node.js to achieve lightweight, modular and fast application. As a result, the startup time of their new app has been reduced by 70% Linkedin: When compared with the previous Ruby on Rails based version, the new mobile app is up to 20 times faster Walmart: %55 of all traffic on Black Friday went to Node servers. Not a single Node server went down
  • 5.
    Success Stories PayPal: Builda Node version of a Java app in half the time with fewer developers. Node version of the app doubled the number of requests/second. Request time dropped 35% Groupon: In comparison with earlier Ruby on Rails platform, web pages got faster by about 50% and are able to serve much higher traffic GoDaddy: 10x fewer servers to host our customer websites and we reduced the Time To First Byte (TTFB) considerably from ~60ms to something around ~12ms
  • 6.
  • 7.
    NodeJs Built on Chrome’sJavascript Runtime (V8) Single thread, event oriented Event-Driven with Non-blocking I/O Use a Event Loop for Non-blocking I/O Can handle thousands of concurrent connections with minimal overhead (CPU/Memory) 40% JS and 60% C++
  • 8.
    Traditional Server New threadper request The thread is blocked during IO operations
  • 9.
    Thesis IO is expensive Threadper connection is memory expensive
  • 10.
    Architecture Client Event loop (main thread) C++ Threadpool (worker threads) Clientssend HTTP requests to Node.js server An Event-loop is woken up by OS, passes request and response objects to the thread-pool Long-running jobs run on worker threads Response is sent back to main thread via callback Event loop returns result to client
  • 11.
    Architecture API in Javascript Relieson Google’s V8 runtime engine Node Bindings allow for server operations (IO, network) Libuv responsible for both asynchronous I/O & event loop
  • 12.
    When to useNodeJs? Highly Event driven & Heavily I/O bound Chat application Online game Collaboration tool Monitoring Dashboard Trader’s Dashboard
  • 13.
    When not touse NodeJs? Heavy CPU intensive calculations on server-side Concurrent Task based applications
  • 14.
  • 15.
    Developement Environment Web Storm SublimeText Atom Nodeclipse Visual Studio Visual Studio Code
  • 16.
  • 17.
    Node Frameworks &Tools NPM (Package Manager) Gulp / Grunt Express.js Frameworks built on Express.js (LoopBack / MEAN / Sails) Restify Mongoose Socket.io
  • 18.
    NPM Package manager forNode Bundled and installed automatically with the environment Frequently Usage • npm install --save package_name • npm update How Does it works? • Read package.json • Installs the dependencies in the local node_modules folder • In global modes, it make a node module accessible to all
  • 19.
    Express.js Web application framework Minimaland Flexible Many Framework based on Express Features • MVC • Routing • Session Support • Cookie Parsing • Middleware (Request-Response Cycle) • View Engines (Jade, Ejs, Vash)
  • 20.
    Vash Familiar Razor engineexperience No special template syntax to learn Syntax:
  • 21.
    Mongoose Object Data Modellingfor Node.js Officially supported by 10gen,Inc Features: • Async and sync validation of models • Model casting • Object lifecycle management (hook save / validate) • Query Builder • Schema Deriven Design
  • 22.
  • 24.
    EventEmitter Most of built-inNode libraries use it Use Observer Pattern
  • 25.
    Socket.io Realtime application framework Crossbrowser (Web socket + fallback) Bi-directional communication Automatic compression IOS, Android, NativeScript support Two parts • Client side (run on browser) • Server side (run on Node)
  • 26.
  • 27.
  • 28.

Editor's Notes

  • #3 Ryan is an American freelance programmer living in of Germany. His work invariably involves interruptible parsers, event loops, and response time histograms. He is the creator of several open source projects including the Ebb web server and the "EY" load balancer module for Nginx. Joyent, ABD'nin San Fransisco kenti merkezli yazılım firması.
  • #5 9 Famous Apps Built with Node.js https://brainhub.eu/blog/2016/05/30/9-famous-apps-using-node-js/
  • #6 9 Famous Apps Built with Node.js https://brainhub.eu/blog/2016/05/30/9-famous-apps-using-node-js/
  • #11 Single thread to handle callbacks Ideal for waiting for I/O calls (network, filesystem, database) Not ideal for CPU intensive operations -> solution fork new child process No thread management No need to synchronization Creating scalable web servers is easy
  • #12 The request to read the file goes through Node bindings to libuv. Then libuv gives the task of reading the file to a thread. When the thread completes reading the file into the buffer, the results goes to V8 and then through the Node bindings in the form of a callback function. In the callback shown the data argument is a Buffer with the file data.
  • #16 Nodeclipse: Built on Eclipse
  • #17 Single Thread
  • #18 Restify = Rest API comes with automatic DTrace support for all your handlers, if you're running on a platform that supports Dtrace Dtrace is a comprehensive dynamic tracing framework created by Sun Microsystems for troubleshooting kernel and application problems on production systems in real time
  • #20 You can set request time before routing on middleware
  • #21 Jade / EJS = http://wildermuth.com/2014/3/24/Choose_Your_Own_Adventure_with_Node_js_View_Engines Vash = https://www.upnxt.com/blog/node-with-express-vash-sqlite http://kirbysayshi.com/2012/04/17/vash.html
  • #22 Table = Collection Record = Document http://www.slideshare.net/yuriybogomolov/ybo-mongo-dbmeetup
  • #23 Sample ExpressJs http://getbootstrap.com/css/#forms http://getbootstrap.com/components/#list-group http://getbootstrap.com/components/#glyphicons-examples
  • #25 You can crete your Node Libraries build on EventEmitter EventEmitter functions : addListener(event, listener) on(event, listener) once(event, listener) removeListener(event, listener) removeAllListeners([event]) setMaxListeners(n) listeners(event) listenerCount(emitter, event)
  • #26 Web socket is not fully supported in all browser yet. İf Web socket is available, we will use it, if it is not, it will try fallback transports until one of them works Fallback tranports: Web sockets Flash sockets Ajax long polling Ajax streaming İframe Json polling
  • #27 Socket.io
  • #28 https://gist.github.com/ilyaigpetrov/f6df3e6f825ae1b5c7e2 https://tipaltilabs.com/2016/07/09/node-js-vs-iis-webapi-async/ OWIN idea on Asp.net after Nodejs
  • #30 http://www.wintellect.com/devcenter/jlane/five-reasons-asp-net-developers-should-care-about-node-js http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx