• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
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

on

  • 5,363 views

 

Statistics

Views

Total Views
5,363
Views on SlideShare
5,131
Embed Views
232

Actions

Likes
11
Downloads
158
Comments
0

6 Embeds 232

http://www.web2expo.com 171
https://blog.spawar.navy.mil 54
https://twitter.com 3
http://paper.li 2
http://webcache.googleusercontent.com 1
http://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

CC Attribution-ShareAlike LicenseCC Attribution-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

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 Presentation Transcript

  • A language for the Internet Why JavaScript and Node.js is right for Internet ApplicationsTom Hughes-Croucher@sh1mmer
  • Me(@sh1mmer)
  • Scalable Server-Side Code with JavaScriptNode Up and Running Tom Hughes-Croucher
  • 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 features More usersMore devices More data
  • Cost Stuff
  • How do we cope withthe increase in demand?
  • Internet Applications
  • How about search?
  • Browser Server Spidering... The Web
  • Would take forever!
  • Browser Front-end ServerComputation Database
  • Computational ComputingClient → Server Computation
  • Internet ComputingClient → Server Server → DB Computation Computation
  • “Traditional” Approach
  • Server
  • Request
  • U L LF
  • Event-driven Approach
  • Place-holder
  • Shared WorkResources
  • 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 Worldn);}).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 Worldn);})//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 Worldn);});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 Worldn);});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 Worldn);});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 Worldn); }); 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 Worldn); }); 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 Worldn);});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 Worldn);});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 Worldn);});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 wewaiting for?
  • "Blocking" is as bad as stopping
  • Event Loop vs. Threads
  • PHP8mb 8gb ram 8000/8 = 1000~1000 users per machine
  • Node.js TCP = 2kb HTTP = 6kb 8gb ram 8000/0.006 = 1.3m1.3m/2 ~ 650k users
  • Apache vs NGINXconcurrency × reqs/sechttp://blog.webfaction.com/a-little-holiday-present
  • Apache vs NGINXconcurrency × memoryhttp://blog.webfaction.com/a-little-holiday-present
  • Node.js is designed forcommunication, just like your applications
  • Thanks!
  • Follow me @sh1mmerTom Hughes-Croucher@sh1mmer