Slide 1

Slide 1 text

3 JANUARY 2013 / Mike Brevoort @mbrevoort

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

ME

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

NODE.JS & ME Met in Spring 2010 Production apps since Winter 2011 Generally an app maker, not a module author

Slide 6

Slide 6 text

WHAT WAS THE ATTRACTION?

Slide 7

Slide 7 text

WHAT IS IT GOOD FOR? (in my opinion) photo by Joriel "Joz" Jimenez

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

WHAT IS IT NOT SO GOOD FOR? (in my opinion)

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

OTHERWISE, VERY NICE!

Slide 12

Slide 12 text

SOME EXAMPLE USES

Slide 13

Slide 13 text

AGGREGATION

Slide 14

Slide 14 text

CHATTY CLIENT WITHOUT AGGREGATION

Slide 15

Slide 15 text

WITH AGGREGATION

Slide 16

Slide 16 text

APP AIR DROP Deep linking, reduce round trips and latency

Slide 17

Slide 17 text

STREAMING IMAGE PROCESSING Nothing buffered to disk

Slide 18

Slide 18 text

SPINDRIFT Dynamic load balancing and deployment * hoping to open source components early this year

Slide 19

Slide 19 text

REALTIME DASHBOARDS Statsd/Graphite inspired by Tasseo

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

FOR EXAMPLE

Slide 22

Slide 22 text

NODE.JS EVALUATOR For this presentation Execute inline code snippets in a spawned node.js process Communicate via socket.io between process and browser

Slide 23

Slide 23 text

FOR EXAMPLE console.log(process.version);

Slide 24

Slide 24 text

THE CLIENT CODE 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]) }

console.log(process.pid);

Slide 25

Slide 25 text

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); }); }

Slide 26

Slide 26 text

YOU'RE NOT IN THE BROWSER ANYMORE Fast forward to the future!

Slide 27

Slide 27 text

LET'S LOOK AT SOME EASY TO FORGET FEATURES YOU CAN USE TODAY!

Slide 28

Slide 28 text

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);

Slide 29

Slide 29 text

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])

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

HARMONY PREVIEW

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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));

Slide 36

Slide 36 text

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));

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

THANKS! QUESTIONS? Mike Brevoort @mbrevoort