A language for the Internet: Why JavaScript and Node.js is right for Internet Application
Upcoming SlideShare
Loading in...5
×
 

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

on

  • 8,513 views

Increasingly we want to do more with the web and Internet applications we build. We have more features, more data, more users, more devices and all of it needs to be in real-time. With all of these ...

Increasingly we want to do more with the web and Internet applications we build. We have more features, more data, more users, more devices and all of it needs to be in real-time. With all of these demands how can we keep up? The answer is choosing a language and a platform that are optimized for the kind of architecture Internet and web applications really have. The traditional approach prioritises computation, assigning server resources before they are actually needed. JavaScript and Node.js both take an event driven approach only assigning resources to events as they happen. This allows us to make dramatic gains in performance and resource utilization while still having an environment which is fun and easy to program.

Statistics

Views

Total Views
8,513
Views on SlideShare
8,486
Embed Views
27

Actions

Likes
16
Downloads
195
Comments
0

11 Embeds 27

http://paper.li 9
https://twitter.com 3
http://edicolaeuropea.blogspot.com 3
http://j.iplsc.com 3
http://j-alfa.iplsc.com.rc.mailv1 3
https://si0.twimg.com 1
http://trunk.ly 1
http://www.onlydoo.com 1
http://drizzlin.com 1
http://www.mongodb.org 1
http://twitter.com 1
More...

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

A language for the Internet: Why JavaScript and Node.js is right for Internet Application A language for the Internet: Why JavaScript and Node.js is right for Internet Application Presentation Transcript

  • A language for the Internet Why JavaScript and Node.js is right for Internet ApplicationsTom Hughes-Croucher@sh1mmer
  • Me
  • Scalable Server-Side Code with JavaScriptNode Up and Running Tom Hughes-Croucher
  • 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 Server Database
  • Client → Server Computation
  • Client → 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! Questions?Tom Hughes-Croucher@sh1mmer