Your SlideShare is downloading. ×
  • Like

Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

How to stop writing spaghetti code

  • 14,386 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • nice work...some more reference to understand event driven I/O with metaphors at http://code.danyork.com/2011/01/25/node-js-doctors-offices-and-fast-food-restaurants-understanding-event-driven-programming/
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
14,386
On SlideShare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
229
Comments
1
Likes
17

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide















































Transcript

  • 1. Ya ho o! In c
  • 2. What is the event loop?
  • 3. Multi-tasking, one thing at a time.
  • 4. 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/');
  • 5. 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'
  • 6. 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
  • 7. 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
  • 8. 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.
  • 9. 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
  • 10. 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
  • 11. 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.)
  • 12. Breaking the event loop
  • 13. 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');
  • 14. Why non-blocking matters
  • 15. var result = db.query("select * from T"); // use result
  • 16. What are we waiting for?
  • 17. "Blocking" is as bad as stopping
  • 18. Event Loop vs. Threads
  • 19. Apache vs NGINX concurrency × reqs/sec http://blog.webfaction.com/a-little-holiday-present
  • 20. Apache vs NGINX concurrency × memory http://blog.webfaction.com/a-little-holiday-present
  • 21. The less you block the more I/O Node can do
  • 22. Programming Style
  • 23. What kinds of applications do we build?
  • 24. Applications • Command line applications • Web servers • Streaming servers • Peer-to-peer
  • 25. Generalising • 'Main' scripts • Request/Response driven • Streaming
  • 26. 'Main' Scripts • Simple JavaScript • Doesn't use anything blocking • Doesn't use callbacks • Almost certainly a simple CLI script
  • 27. Request/Response Driven • Discrete request/response cycle • Encapsulated requests/responses
  • 28. Streaming • Arbitrary IN/OUT data • Stream data may not be encapsulated
  • 29. 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/'
  • 30. http.createServer(function (req, res) { })
  • 31. //Server CommonJS Module Skeleton var sys = require('sys'), EE = require('events').EventEmitter; var Server = exports.Server = function() { //Server Setup }; sys.inherits(Server, EE); Server.prototype.Request = function() { //Request Setup }; sys.inherits(Sever.prototype.Request, EE); Server.prototype._createRequest = function(req, res) { //Request Factory return new this.Request(); }; exports.createServer = function() { //Server Factory return new Server(); };
  • 32. var server = Server.createServer(); server.on('request', server._createReq);
  • 33. Step 1. Create new Server
  • 34. Step 2. Listen for requests
  • 35. Step 3. On request create new Request
  • 36. Step 4. Encapsulate request state in request object
  • 37. Step 5. Request life-cycle
  • 38. Closure Encapsulation DB File Query dbResponse Access Init fileReady Server Request Init Request Respond Process Processed
  • 39. Summary • Event Loop does 1 thing at a time • Until a loop loops events aren't fired • Emitted events run immediately • Define a "server" structure • Use factory methods to encapsulate with named objects • Use events to maximize the event loop