Save 37% off PRO during our Black Friday Sale! »

Node.js & Me - DenverJS January 2013

Node.js & Me - DenverJS January 2013

Nodes.js: based on my experience where I think it fits well, ways I've used it and an over view of some ECMAScript5 and Harmony features you can use with node.js today.

Presented at DenverJS January 3rd 2013.

7071119714e1a32441aca0c336657d3e?s=128

Mike Brevoort

January 03, 2013
Tweet

Transcript

  1. 3 JANUARY 2013 / Mike Brevoort @mbrevoort

  2. AGENDA Well, it's will be a bit of a hodge

    podge... My thoughts on Node.js Discuss how I've used it Demo some easy to overlook features
  3. ME

  4. ME New York → Delaware/Philadephia → 10th year in Colorado

    Married 5 Children Coding for 19 years JavaScript, Java, Groovy, C/C++, C#, PHP, Python, XQuery Work @ Pearson Co-organizer of DenverJS
  5. NODE.JS & ME Met in Spring 2010 Production apps since

    Winter 2011 Generally an app maker, not a module author
  6. WHAT WAS THE ATTRACTION?

  7. WHAT IS IT GOOD FOR? (in my opinion) photo by

    Joriel "Joz" Jimenez
  8. GOOD I/O bound programs API aggregation, orchestration, proxying Filtering and

    data normalization Edge server for client apps Streaming I/O I/O centric systems programming
  9. WHAT IS IT NOT SO GOOD FOR? (in my opinion)

  10. BAD LESS GOOD Primarily procedural and serial programs CPU bound

    tasks Heavy "business logic" Large code bases that "can't" be modularized Very large teams
  11. OTHERWISE, VERY NICE!

  12. SOME EXAMPLE USES

  13. AGGREGATION

  14. CHATTY CLIENT WITHOUT AGGREGATION

  15. WITH AGGREGATION

  16. APP AIR DROP Deep linking, reduce round trips and latency

  17. STREAMING IMAGE PROCESSING Nothing buffered to disk

  18. SPINDRIFT Dynamic load balancing and deployment * hoping to open

    source components early this year
  19. REALTIME DASHBOARDS Statsd/Graphite inspired by Tasseo

  20. WEBSOCKETS + FALLBACKS Server Browser Pub Sub (Socket.io)

  21. FOR EXAMPLE

  22. NODE.JS EVALUATOR For this presentation Execute inline code snippets in

    a spawned node.js process Communicate via socket.io between process and browser
  23. FOR EXAMPLE console.log(process.version);

  24. THE CLIENT CODE <script src="/socket.io/socket.io.js"></script> <script> </script> var socket =

    io.connect(); socket.on('result', function (data) { humane.log(data, { timeout: 5000, clickToClose: true, addnCls: 'humane-bigbox' }) }); function runit(elemId) { socket.emit('run', ['-e', document.getElementById(elemId).textContent]) } <pre> <code contenteditable id="code_evaluator"> console.log(process.pid); </code> </pre> <a onclick="runit('code_evaluator')"> <img src="img/run-dmc.jpg"/> </a>
  25. SERVER CODE var spawn = require('child_process').spawn , express = require('express')

    , app = express() , server = require('http').createServer(app) , io = require('socket.io').listen(server); app.use(express.static(__dirname + '/../reveal.js')); server.listen(4444); io.sockets.on('connection', function (socket) { socket.on('run', function (data) { runit(data, function (output) { socket.emit('result', output); }) }); }); function runit(options, cb) { var run = spawn('node', options) , output = ""; var append = function (val) { output += val }; run.stdout.on('data', append); run.stderr.on('data', append); run.on('exit', function (code) { if (code !== 0) output += 'EXITED WITH CODE: ' + code; cb(output); }); }
  26. YOU'RE NOT IN THE BROWSER ANYMORE Fast forward to the

    future!
  27. LET'S LOOK AT SOME EASY TO FORGET FEATURES YOU CAN

    USE TODAY!
  28. ECMASCRIPT 5 Getters and Setters var obj = { get

    name() { return this._name }, set name(val) { this._name = val.toUpperCase(); } } obj.name = "mike"; console.log(obj.name);
  29. ECMASCRIPT 5 Array.prototype Still using underscore.js for this stuff? indexOf(value);

    lastIndexOf(value) filter(callback) forEach(callback) every(callback) map(callback) some(callback) reduce(callback[, initialValue]) reduceRight(callback[, initialValue])
  30. ECMASCRIPT 5 Object.seal(obj) Prevents adding or deleting properties Or changing

    the descriptors of any property Property values can be changed however var o = { a: 1 }; Object.seal(o); console.log("o is sealed? ", Object.isSealed(o)); o.b = 2; // BOOM
  31. ECMASCRIPT 5 Object.freeze(obj) Same as seal but properties can't be

    changed either var o = { a: 1 }; Object.freeze(o); console.log("o is freezed? ", Object.isFrozen(o)); o.a = 2; // BOOM
  32. ECMASCRIPT 5 Object.create Creates a new object with the specified

    prototype object and properties var obj = Object.create({x: 10, y: 20}, { z: { value: 30, writable: true }, sum: { get: function() { return this.x + this.y + this.z; } } }); log(obj.x); // 10 log(obj.y); // 20 log(obj.z); // 30 log(obj.sum); // 60
  33. HARMONY PREVIEW

  34. V8 HARMONY OPTIONS node --v8-options | grep -A 1 harmony

    --harmony_typeof (enable harmony semantics for typeof) type: bool default: false --harmony_scoping (enable harmony block scoping) type: bool default: false --harmony_modules (enable harmony modules (implies block scoping )) type: bool default: false --harmony_proxies (enable harmony proxies) type: bool default: false --harmony_collections (enable harmony collections (sets, maps, a nd weak maps)) type: bool default: false --harmony (enable all harmony features (except typeof)) type: bool default: false
  35. HARMONY COLLECTIONS Maps (and WeakMaps) Harmony Iterators not implemented yet

    :( var assert = require('assert') , map = new Map() , obj = new Object() , foo = { a: 1 } , bar = { a: 1 }; obj[foo] = 'foo'; assert.equal('foo', obj[foo]); obj[bar] = 'bar'; assert.equal('bar', obj[bar]); assert.equal('bar', obj[foo]); // ??? -> keys converted to strings map.set(foo, 'foo'); assert.equal('foo', map.get(foo)); map.set(bar, 'bar'); assert.equal('bar', map.get(bar)); assert.equal('foo', map.get(foo));
  36. HARMONY COLLECTIONS Sets Harmony Iterators not implemented yet :( var

    assert = require('assert') , set = new Set() , foo = { a: 1 } , bar = { a: 1 } , baz = { a: 1 }; set.add(foo); set.add(bar); assert(set.has(foo)); assert(set.has(bar)); assert(!set.has(baz)); set.delete(foo); assert(!set.has(foo)); assert(set.has(bar));
  37. BLOCK SCOPE const let node --harmony --use-strict var assert =

    require('assert'); const baz = 'baz'; assert.equal('baz', baz); baz = 'new baz'; // BOOM! // SyntaxError: Assignment to constant variable. var assert = require('assert'); { var foo = 'foo'; let bar = 'bar'; } assert.equal('foo', foo); assert.equal('bar', bar); // BOOM! // ReferenceError: bar is not defined
  38. THANKS! QUESTIONS? Mike Brevoort @mbrevoort