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

Post on 29-Jan-2018

8,364 views 0 download

transcript

A language for the Internet

Why JavaScript and Node.js is right for Internet Applications

Tom Hughes-Croucher@sh1mmer

Me(@sh1mmer)

Scalable Server-Side Code with JavaScript

Tom Hughes-Croucher

NodeUp and Running

A brief aside.A small lecture on

biology

The common tree shrew

Diana Monkey

Back to your feature presentation.

Internet?

She’s called Eleanor

More featuresMore users

More devicesMore data

Stuff

Cost

How do we cope with the increase in demand?

Internet Applications

How about search?

Browser

Server

Spidering... The Web

Would take forever!

Browser

Front-endServer

Database

Computation

Client → Server

Computation

Computational Computing

Client → Server Server → DB

Computation Computation

Internet Computing

“Traditional” Approach

Server

Request

FULL

Event-driven Approach

Place-holder

SharedWork

Resources

Welcome to Node.js

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

$Enki:~ $ node

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

> 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:~ $

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)

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/');

var http = require('http');

//include the http library

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

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

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

Why is Node.js suited to Internet Apps?

What is the event loop?

Multi-tasking, one thing at a time.

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/');

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'

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

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

*

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.

*

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

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

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.)

Breaking the event loop

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');

Why non-blocking matters

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

What are we waiting for?

"Blocking" is as bad as stopping

Event Loop vs. Threads

8mb

PHP

8gb ram8000/8 = 1000

~1000 users per machine

8gb ram8000/0.006 = 1.3m

1.3m/2 ~ 650k users

Node.jsTCP = 2kb

HTTP = 6kb

Apache vs NGINXconcurrency ! reqs/sec

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

Apache vs NGINXconcurrency ! memory

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

Node.js is designed for communication, just like

your applications

Thanks!

Follow me @sh1mmer

Tom Hughes-Croucher@sh1mmer