class: center, middle # Intro to Node.js The V8 non-blocking asynchronous JavaScript runtime. --- class: center, middle # Intro to Node.js The ....... Whaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa? --- # What is Node.js? --- # What is Node.js? **A set of non-blocking IO bindings for the V8 JavaScript VM:** --- # What is Node.js? **~~A set of non-blocking~~ IO bindings for ~~the~~ V8 ~~JavaScript VM~~:** --- # What is Node.js? ** IO bindings for V8 :** --- # What is Node.js? ** IO bindings for V8 :** - **V8** --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr - database access --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr - database access - **They're asynchronous!** --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr - database access - **They're asynchronous!** - IO runs in parallel by default --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr - database access - **They're asynchronous!** - IO runs in parallel by default - **They're fast!** --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr - database access - **They're asynchronous!** - IO runs in parallel by default - **They're fast!** - V8 & Node.js are written in C/C++ --- # What is Node.js? ** IO bindings for V8 :** - **V8** - Google Chrome's JavaScript *Virtual Machine* - **Node.js** - IO bindings for V8: - filesystem - networking - stdin/stdout/stderr - database access - **They're asynchronous!** - IO runs in parallel by default - **They're fast!** - V8 & Node.js* are written in C/C++ \* *mostly* --- # Demo: Hello World ```bash # REPL $ node > console.log('Hello world') Hello World . ``` --- # Demo: Hello World ```bash # REPL $ node > console.log('Hello world') Hello World # File $ echo "console.log('Hello world')" > foo.js $ node foo.js Hello World . ``` --- # Demo: Hello World ```bash # REPL $ node > console.log('Hello world') Hello World # File $ echo "console.log('Hello world')" > foo.js $ node foo.js Hello World # Eval $ node -e "console.log('Hello world')" Hello World ``` --- # Why Node.js? --- # Why Node.js? **How is node.js different?** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:**
1. **JavaScript:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:**
1. **JavaScript:**
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded
1. **JavaScript:**
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes
1. **JavaScript:**
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:**
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript - Share client/server code
1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript - Share client/server code - Server-side Rendering 1. ***Single-threaded* Event-loop:** --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript - Share client/server code - Server-side Rendering 1. ***Single-threaded* Event-loop:** - NGINX --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript - Share client/server code - Server-side Rendering 1. ***Single-threaded* Event-loop:** - NGINX - Netty or Reactor (Java) --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript - Share client/server code - Server-side Rendering 1. ***Single-threaded* Event-loop:** - NGINX - Netty or Reactor (Java) - Twisted or Tornado (Python) --- # Why Node.js? **How is node.js different?** 1. **Asynchronous non-blocking IO:** - All IO calls are backgrounded - Execution resumes when IO completes - No legacy blocking APIs (No IO in V8) 1. **JavaScript:** - Unifies the front-end & mid-tier - Universal JavaScript - Share client/server code - Server-side Rendering 1. ***Single-threaded* Event-loop:** - NGINX - Netty or Reactor (Java) - Twisted or Tornado (Python) - EventMachine (Ruby) --- # Blocking vs Non-Blocking **Blocking Code:** ```html Read file from Filesystem Print data Do something unrelated ``` **Non-Blocking Code:** ```html Read file from Filesystem // This is asynchronous On completion, Print data Do something unrelated ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js Read file from Filesystem Print data Do something unrelated ``` **Non-Blocking Code:** ```js Read file from Filesystem // This is asynchronous On completion, Print data Do something unrelated ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js Read file from Filesystem console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js Read file from Filesystem // This is asynchronous console.log(data) doSomethingUnrelated() ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js let data = // Read file from Filesystem console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js let data = // Read file from Filesystem // This is asynchronous console.log(data) doSomethingUnrelated() ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js let data = fs.readFile ('~/.bashrc') console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js let data = fs.readFile('~/.bashrc') // This is asynchronous console.log(data) doSomethingUnrelated() ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js let data = fs.readFileSync('~/.bashrc') console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js let data = await fs.readFile('~/.bashrc') // This is asynchronous console.log(data) doSomethingUnrelated() ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js let data = fs.readFileSync('~/.bashrc') console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js let data = await fs.readFile('~/.bashrc') // This is asynchronous console.log(data) doSomethingUnrelated() ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js let data = fs.readFileSync('~/.bashrc') console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js async function readFile() { let data = await fs.readFile('~/.bashrc') console.log(data) } readFile() doSomethingUnrelated() ``` --- # Blocking vs Non-Blocking **Blocking Code:** ```js // BAD let data = fs.readFileSync('~/.bashrc') console.log(data) doSomethingUnrelated() ``` **Non-Blocking Code:** ```js // GOOD async function readFile() { let data = await fs.readFile('~/.bashrc') console.log(data) } readFile() doSomethingUnrelated() ``` --- # In Summary: Blocking ```js function readFileSync() { let data = fs.readFileSync('~/.bashrc') console.log(data) } readFileSync() doSomethingUnrelated() ``` --- # In Summary: Non-Blocking ```js async function readFile () { let data = await fs.readFile ('~/.bashrc') console.log(data) } readFile () doSomethingUnrelated() ``` --- class: center, middle ### Always remember... # **`async` & `await`**
--- class: center, middle ### Always remember... # **`async` & `await`** *or else...* --- # Blocking vs Non-Blocking ```javascript async function readFile(filePath) { // Use await for serial execution let data = await fs.readFile(filePath) console.log(data) } readFile('~/.bashrc') readFile('~/.bash_profile') ``` [](./images/blocking_vs_nonblocking.png) --- # Parallel vs Serial ```javascript async function swapFiles(filePathA, filePathB) { // Execute reads in parallel let [dataA, dataB] = await Promise.all([ fs.readFile(filePathA), fs.readFile(filePathB) ]) // Execute writes in serial await fs.writeFile(filePathA, dataB) await fs.writeFile(filePathB, dataA) } ``` [](./images/blocking_vs_nonblocking.png) --- # When Node.js? --- # When Node.js? **When should I use node.js:** --- # When Node.js? **When should I use node.js:** - IO-bound applications --- # When Node.js? **When should I use node.js:** - ~~IO-bound applications~~ --- # When Node.js? **When should I use node.js:** - High-concurrency (many simultaneous IO requests) --- # When Node.js? **When should I use node.js:** - High-concurrency (many simultaneous IO requests) - Low-latency --- # When Node.js? **When should I use node.js:** - High-concurrency (many simultaneous IO requests) - Low-latency - Unified JavaScript "front-end" --- # When Node.js? **When should I use node.js:** - High-concurrency (many simultaneous IO requests) - Low-latency - Unified JavaScript "front-end" = mid-tier + client --- # When Node.js? **When should I use node.js:** - High-concurrency (many simultaneous IO requests) - Low-latency - Unified JavaScript "front-end" = mid-tier + client*
\* Also, iOS & Android with React Native --- # When Node.js? **When should I use node.js:** - High-concurrency (many simultaneous IO requests) - Low-latency - Unified JavaScript "front-end" = mid-tier + client* - DevOps, tooling & other scripting environments \* Also, iOS & Android with React Native --- # Where Node.js --- # Where Node.js **What should you build?** --- # Where Node.js **What should you build?** Anything that's **IO**-bound: --- # Where Node.js **What should you build?** Anything that's **~~IO~~**-bound: --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: - Realtime Apps - *Websockets, Chat* --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: - Realtime Apps - *Websockets, Chat* - Fast File Upload Server - *Especially large files* --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: - Realtime Apps - *Websockets, Chat* - Fast File Upload Server - *Especially large files* - Ad/Metrics Server --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: - Realtime Apps - *Websockets, Chat* - Fast File Upload Server - *Especially large files* - Ad/Metrics Server - Tooling CLIs - *DevOps, CI, CD, QA* --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: - Realtime Apps - *Websockets, Chat* - Fast File Upload Server - *Especially large files* - Ad/Metrics Server - Tooling CLIs - *DevOps, CI, CD, QA* - API Server - *Mobile back-end* --- # Where Node.js **What should you build?** Anything that's **NOT CPU**-bound: - Realtime Apps - *Websockets, Chat* - Fast File Upload Server - *Especially large files* - Ad/Metrics Server - Tooling CLIs - *DevOps, CI, CD, QA* - API Server - *Mobile back-end* - Unified "Front-end" - *Any Web Application!* --- class: center, middle # Questions?