robrighter's Node.js presentation for DevChatt

1,653 views

Published on

Getting started with node.js using http://current.thisismedium.com as a sample application.

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,653
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide






































  • robrighter's Node.js presentation for DevChatt

    1. 1. Current @robrighter
    2. 2. What is Node.js? _ non-blocking _ evented _ javascript @robrighter
    3. 3. Why is node useful? @robrighter
    4. 4. Why is node useful? It can handle a ton of concurrent connections @robrighter
    5. 5. A Gross Oversimplification of HTTP Give me a page Sure Connection Closed @robrighter
    6. 6. AJAX Polling Got any updates? No Connection Closed Got any updates? No Connection Closed @robrighter
    7. 7. Comet Got any updates? ...wait Yes Connection Closed @robrighter
    8. 8. Websocket asyncronous updates @robrighter
    9. 9. Evented Programming @robrighter
    10. 10. Non-Evented Example mysql_select_db("example", $con); $result = mysql_query("SELECT * FROM user WHERE fname='Bill'"); while($row = mysql_fetch_array($result)){     echo $row['fname']; } @robrighter
    11. 11. Evented Example fs.readFile("./file", function (err, data) { if (err) throw err; sys.puts(data); }); @robrighter
    12. 12. An Example @robrighter
    13. 13. Server Side @robrighter
    14. 14. Setup the Data Source var rb = new lpb.LongPollingBuffer(70); var dump = process.createChildProcess("tcpdump",["-i","en1","-A","-n","port", "80"]); var ignorelist = ['localhost','foxnews.com']; //Setup the listener to handle the flow of data from the dump dump.addListener("output", function (data) {     var hosts = data.match(/Host: (.*)/g);     if(hosts){         _.each(hosts, function(item){             var out = item.slice(6);             sys.puts(out);             if(!_.detect(ignorelist, function(s){ return (item.indexOf(s) > -1); })){                 rb.push(out);             }                      });     } }); @robrighter
    15. 15. LongPollingBuffer DATA ARRAY DATA: 1 1 1 0 1 0 0 1 1 0 OFFSET: 0 2 3 4 5 6 7 8 9 10 CLIENT ONE CLIENT TWO @robrighter
    16. 16. Setup the Updater fu.get("/update", function (req, res) { //Send the HTTP Headers       res.sendHeader(200,{"Content-Type": "text/html"});        //Parse out the since from the query string var thesince;       if(url.parse(req.url,true).hasOwnProperty('query') && url.parse(req.url,true).query.hasOwnProperty('since')){           thesince = parseInt(url.parse(req.url,true)['query']['since']);       }       else {           thesince = -1;       } //Setup the datalistener (note why this works so well with JS scoping)       rb.addListenerForUpdateSince(thesince, function(data){            var body = '['+_.map(data,JSON.stringify).join(',n')+']';            res.write( body );            res.close();       }); }); @robrighter
    17. 17. Client Side @robrighter
    18. 18. Long Polling Request var url = “/update?since=” + lastrecieved; $.getJSON(url, function(data){ var itemstoadd = _.map(data.reverse(),function(item){ lastrecieved = item["offset"]; return domainmarkup(item['value']); });   var uc = $("#updatedcontent"); _.map(itemstoadd, function(item){ if(!interrupt){ uc.prepend(item); } }); setTimeout('getupdate(lastrecieved)', 1000); }); @robrighter
    19. 19. Questions? github.com/robrighter twitter.com/robrighter @robrighter

    ×