a language for the internet: why javascript and node.js is right for internet applications

83
A language for the Internet Why JavaScript and Node.js is right for Internet Applications Tom Hughes-Croucher @sh1mmer

Upload: tom-croucher

Post on 29-Jan-2018

8.364 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

A language for the Internet

Why JavaScript and Node.js is right for Internet Applications

Tom Hughes-Croucher@sh1mmer

Page 2: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Me(@sh1mmer)

Page 3: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 4: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 5: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Scalable Server-Side Code with JavaScript

Tom Hughes-Croucher

NodeUp and Running

Page 6: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

A brief aside.A small lecture on

biology

Page 7: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

The common tree shrew

Page 8: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Diana Monkey

Page 9: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 10: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 11: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Back to your feature presentation.

Page 12: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Internet?

Page 13: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 14: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 15: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

She’s called Eleanor

Page 16: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 17: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 18: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 19: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 20: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 21: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

More featuresMore users

More devicesMore data

Page 22: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Stuff

Cost

Page 23: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

How do we cope with the increase in demand?

Page 24: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Internet Applications

Page 25: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

How about search?

Page 26: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Browser

Server

Spidering... The Web

Page 27: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Would take forever!

Page 28: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Browser

Front-endServer

Database

Computation

Page 29: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Client → Server

Computation

Computational Computing

Page 30: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Client → Server Server → DB

Computation Computation

Internet Computing

Page 31: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

“Traditional” Approach

Page 32: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 33: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Server

Page 34: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Request

Page 35: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 36: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 37: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

FULL

Page 38: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Event-driven Approach

Page 39: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Place-holder

Page 40: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 41: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

SharedWork

Resources

Page 42: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Welcome to Node.js

Page 43: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Node.js?

• Server Side JavaScript runtime

• Built on top of V8 JavaScript engine from Google Chrome

• Non-blocking I/O APIs

• Easy to extend APIs and modules

Page 44: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

$Enki:~ $ node

Page 45: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

$Enki:~ $ node> 3 > 2 > 1false> true == 1true> true === 1false

Page 46: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

> console.log('Hello World');Hello World> .help.clear Break, and also clear the local context..exit Exit the prompt.help Show repl options> .clearClearing context...> .exitEnki:~ $

Page 47: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Enki:~ $ node> var foo = "bar";> foo;'bar'> .clearClearing context...> fooReferenceError: foo is not defined at [object Context]:1:1 at Interface.<anonymous> (repl:98:19) at Interface.emit (events:27:15) at Interface._ttyWrite (readline:295:12) at Interface.write (readline:132:30) at Stream.<anonymous> (repl:79:9) at Stream.emit (events:27:15) at IOWatcher.callback (net:489:16)

Page 48: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Page 49: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');

//include the http library

Page 50: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

http.createServer(function (req, res) {

}).listen(8124, "127.0.0.1");

//create an http server//when ‘stuff’ happens call this anonymous function//listen on port 8124 of the IP 127.0.0.1

Page 51: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');})

//when ‘stuff’ happens my function fires//I get a request object and a response object//I write to the response object header//HTTP status 200 and content-type ‘text/plain’//close the response with the body://Hello World

Page 52: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

console.log('Server running at http://127.0.0.1:8124/');

//write Server is running at http://127.0.0.1:8124///to the console

Page 53: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Why is Node.js suited to Internet Apps?

Page 54: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 55: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

What is the event loop?

Page 56: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 57: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Multi-tasking, one thing at a time.

Page 58: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 59: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 60: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Page 61: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 1.Evaluate 'Main'

Page 62: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 1.variables: http -> http module server -> http server

listeners: server.request -> function

Page 63: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 2.Event Loop

*

Page 64: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 2.Do we have active listeners?

listeners: server.request -> function

Yes! Wait for listeners.

*

Page 65: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 3.Event Calls

Page 66: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 3.'request' is called. Since

listeners: server.request -> function

Call function

Page 67: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var http = require('http');server = http.createServer();

server.on('request', function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');});

server.listen(8124, "127.0.0.1");console.log('Server running at http://127.0.0.1:8124/');

Step 3.Loop!

(go to Step 2.)

Page 68: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Breaking the event loop

Page 69: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 70: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

EE = require('events').EventEmitter;ee = new EE();

die = false;

ee.on('die', function() { die = true;});

setTimeout(function() { ee.emit('die');}, 100);

while(!die) {}

console.log('done');

Page 71: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Why non-blocking matters

Page 72: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

var result = db.query("select * from T"); // use result

Page 73: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

What are we waiting for?

Page 74: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications
Page 75: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

"Blocking" is as bad as stopping

Page 76: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Event Loop vs. Threads

Page 77: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

8mb

PHP

8gb ram8000/8 = 1000

~1000 users per machine

Page 78: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

8gb ram8000/0.006 = 1.3m

1.3m/2 ~ 650k users

Node.jsTCP = 2kb

HTTP = 6kb

Page 79: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Apache vs NGINXconcurrency ! reqs/sec

http://blog.webfaction.com/a-little-holiday-present

Page 80: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Apache vs NGINXconcurrency ! memory

http://blog.webfaction.com/a-little-holiday-present

Page 81: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Node.js is designed for communication, just like

your applications

Page 82: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Thanks!

Page 83: A language for the Internet: Why JavaScript and Node.js is right for Internet Applications

Follow me @sh1mmer

Tom Hughes-Croucher@sh1mmer