Introduction to Node.js at the GfK Nurago and SirValUse Academy 2013

[wp_pdfjs id=1058 ]

Overview

This is an Introduction to Node.js given at the GfK Nurago and SirValUse Academy 2013 in Hamburg. In this talk I try to reason about the use of server-side JavaScript and try to workout what makes Node.js so special – The Event Loop.
At the end of the talk I give some examples of which I think are particular good  use cases for Node.js. I also make some notes about running Node in a productive environment.

For notes

Notes

Slide # 1

  1. Introduce Yourself 😉
    1. Henning Kropp @ GfK Nurago
  2. Who has NOT heard of Node.js before?
    1. Node.js is 5 years old
    2. Node.js hottest tech topic:
      1. We want to find out WHY?

Slide #2

  1. Aim of the Talk:
    1. Motivate the Use of JavaScript
    2. Introduce Node.js
    3. Explain Event Driven / Event Loop
    4. Show Sample Use Cases
    5. Some Notes about Production Node

Slide #3

  • What Do This Companies Have in Common?

Slide #4

  • (Among Other Things ….) => JavaScript
  • Remember the Browser Wars?
  • Race for Fastest Runtime Environment / VM
  • Other Platforms:
    • Microsoft -> .Net
    • Oracle -> Java/JVM
    • Apple -> ObjectC
    • Google -> Go and other???
  • Nashorn Part of JDK8 (maybe)
  • Chakra Part of Windows 8 and Windows RT

Slide #5

  • Dozens of Platforms Support JavaScript!
  • At Nurago:
    • Client a Lot of JavaScript
    • Mobile a Lot of JavaScript
    • Web Pages a Lot of JavaScript
    • …. Server Not so Much… 🙁

Slide #6

  • Last but not Least:
    • Hundreds of Thousands of Websites Developed in JS
    • Thousands of JS Developers

Slide #7

  • Key Facts:
    • JavaScript Most Popular Language
    • Used and Backed by Key IT Companies
    • Thousands of Developers
    • Resources & Ecosystem
    • A lot of Money and Man Years
    • One Language Client/Server
  • Douglas Crockford responsible for maturity
    • JS the Good Parts the Only Book One should Read
    • JSLint for Static Code Analysis

Slide #8

  • Few know JavaScript Part of Server since Beginning
    • Netscape Enterprise Server 1994
    • Also IIS in 90s
  • Since 2008 CommJS specification for not Browser
    • Server-side JavaScript applications
    • Command line tools
    • Desktop GUI-based applications
    • Hybrid applications (Titanium, Adobe AIR)
  • RingoJS -> Java Based
  • Don’t Know Much About Jetpack
  • AND NODE.JS

Slide #9

  • Why Node?
    • Event Driven
    • Non-Blocking
    • Single Threaded!
    • Networking!
  • Need to Understand Modern Web requirements
  • Need to Understand Non-Blocking
  • Need to Understand Event Driven
  • But Before that Have Some Look at Some Code
  • Examples! Code!

Slide #10

  • Node.js is Essentially About Networking!
  • createServer() Does not Block
  • Non-blocking thru Callbacks
  • Callbacks, Functions First-Class Citizens in JS
    • One of the Good Parts
  • Running Apache Benchmark
    • Able to Scale the Webserver on Laptop to 900 Concurrent
  • Next: What Does Single Threaded Mean?

Slide #11

  • Node.js is Single Threaded!
  • How Does this Scale on my Laptop?
  • Trying 10 con. Requests.
  • What Time Do You Expect?
  • Node is Blocking/Single Threaded
  • Node API is Not Blocking
  • File Read/Write … I/O
  • Node Uses the Event Loop
  • Next Simple REST

Slide #12

  • Simple Example of REST
  • Why Would We Need a Networking Framework?
  • Why Event Driven?
  • Have to Look at Modern Web Requirements!

Slide #13

  • This is The Classic Web
  • Process by Order
  • Apache Spawns Process/Thread for Every Request

Slide #14

  • Today Web is a Eventful Web
  • User Want Imitate Responses
  • Small but many Requests
  • For example:
    • Instant Search at Google
    • Every Search at Google with Auto Completion Results in Multiple Requests
  • User Expects this Behaviour

Slide #15

  • Who Ever Had the Pleasure of Scaling a Web App Probably used NginX
  • NginX and Node.js -> Event Driven!
  • NEXT: Lets have a look at the Event Loop!

Slide #16

  • Events are put on the Event Loop
    • Like ‘onRequest
    • One/Many Callbacks per Event
    • A Thread waiting for Request
    • If Requests Occurs, Callback is Run
    • Than Response is Send to Client
  • Everything (I/O) is Asynchronous
    • File reads
    • Database Access
  • Callback Function is Used
  • HOW IS DIFF FROM TRADITIONAL WEBSERVER?

Slide #17

  • Event Loop @ a McDonalds
  • Imagine Ur at the Counter Placing an Order
  • Process/Thread = Cashier
  • Cashier Taking Order and Waits for Burger and Fries
  • One Process for one Order
  • Only after Assemble Order finished -> Next Order/Customer
  • Most the Time Waiting for Burger and Fries
  • Apache Basically Works that Way

Slide #18

  • While We wait for Burger People line Up
  • All have to wait till Cashier is finished w/ One Order
  • Who can We Scale This?

Slide #19

  • We could Create hire more Processes
    • Can get crowded behind the Counter
    • Still Most of the Time Waiting
    • What the Apache is Doing
  • How Does McD Scale This, If a Order Takes Longer Than Certain Time?
    • You get a Number!
    • That’s Event Driven
    • When the Burger is Ready Process Stops What he was Doing and Serves the Burger
    • Process Does Not Wait

Slide #20

  • Node is Useful for I/O!
  • Network, Files, Databases …
  • File Server for Example
  • What of This Code is Async?
  • What of This Code is Blocking?
  • Where is the Event Loop Here?

Slide #21

  • Node.js as a Proxy
  • Most Popular Proxy Module is http-proxy
  • nodejitsu.com Uses http-proxy
  • Not very Readable Example, Lets Skip Details 😉

Slide #22

  • Screen scrpaing – JavaScript End-to-End
  • Jsdom is Slow, Often Used PhantomJS

Slide #23

  • Forever Restarts, and Detaches From Shell
  • Cluster creates Worker for each CPU Core
    • One Node Server/Thread per CPU Core
    • Example later
  • Cloud support with Windows Azure and nodjitsu and many more
  • Node Production Environment Does Some Optimizations
    • Caching
    • And Others

Slide #24

  • Example of Cluster Usage
  • All Workers Run on Same Port

 

Leave a Reply