building businesspost.ie using node.js

34
Building businesspost.ie Richard Rodger @rjrodger Friday 9 November 2012

Upload: richard-rodger

Post on 12-May-2015

1.411 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Building businesspost.ie using Node.js

Buildingbusinesspost.ie

Richard Rodger@rjrodger

Friday 9 November 2012

Page 2: Building businesspost.ie using Node.js

businesspost.ie

Friday 9 November 2012

Page 3: Building businesspost.ie using Node.js

Everything JavaScript.

Friday 9 November 2012

Page 4: Building businesspost.ie using Node.js

So, how do you build it?‣have a common code-base,‣be service-oriented,‣use small, independent pieces.

Friday 9 November 2012

Page 5: Building businesspost.ie using Node.js

Mobile Web Apps

Web Services

API

Cloud Services

Mobile & Tablet Web

Mobile & Tablet Apps

Desktop Web

REST & JSON

Horizontal Scale

Cloud Hosted

Database

Third Party Services

Monitoring

Friday 9 November 2012

Page 6: Building businesspost.ie using Node.js

Client-side•Common code-base

• even for hybrid apps!

•backbone.js

• shims for weak browsers

•browser-targeting: user-agent & capabilities

• responsive layout (mostly)

Models

Views

Router

Helpers

#! URLs

Data, biz logic

DOM Layout

Shared code

Friday 9 November 2012

Page 7: Building businesspost.ie using Node.js

Running...1. Load static index.html and assets

2. Init shared code, then targeted code

3. Init router, and display as per #! URL

4. Wait for events

Friday 9 November 2012

Page 8: Building businesspost.ie using Node.js

Native AppsSame code as mobile web versions, ...

... wrapped using PhoneGap to run natively

... plus some native plugins

Friday 9 November 2012

Page 9: Building businesspost.ie using Node.js

Server-side• nginx & Node.js

• Small code volume

• Third party modules:

• connect

• express

• seneca

• Deploy with:

• sudo killall node

API functions

Helpers

Router

Modules

map /api/ URLs

to functions

Shared code

(some with client)

Open source

heavy-lifting

function( req, res ) { ... }

Friday 9 November 2012

Page 10: Building businesspost.ie using Node.js

Running...1. Connect to databases and listen for HTTP

2. Route HTTP requests to API functions

3. Talk to database, wait for callback

4. Send JSON back to client

Friday 9 November 2012

Page 11: Building businesspost.ie using Node.js

this is the common language of Node.js - always use it!

Callback Style

doSomething( input, function( error, output ) { ...})

Friday 9 November 2012

Page 12: Building businesspost.ie using Node.js

function wraperror( success ) { return function( err, result ) { if( err ) return handleError( err ); else return success( result ); }}

database.doQuery( wraperror( function( result ) { doRealWork( result );}))

use functional style avoidif statements everywhere

Error Pattern

Friday 9 November 2012

Page 13: Building businesspost.ie using Node.js

Database Hosting Monitors

MongoDB

Redis

memcached

Amazon

Load Balancer

Instance Scaling

Amazon

cloudkick

Continuous

Cloud Services

Friday 9 November 2012

Page 14: Building businesspost.ie using Node.js

Analytics

Logging

Email

Integrations...

Twitter

Facebook

LinkedIn

E-Commerce

In-App Purchasing

Stock Feed

JSON, XML, simple form data, text files, ... ... all easy using JavaScript and Node.js Modules

Friday 9 November 2012

Page 15: Building businesspost.ie using Node.js

Lots of Little Processes‣Web API‣RSS reader‣Image resizer

‣Admin site‣Search index‣Sitemap

Friday 9 November 2012

Page 16: Building businesspost.ie using Node.js

Use queues!This is how processes talk.

Friday 9 November 2012

Page 17: Building businesspost.ie using Node.js

redis.publish("images", "/storage/img1234.jpg");

news reader finds an image...... and pushes it onto queue

Example!

redis.subscribe("images");redis.on("message", function(channel, message) { resizeImage( message ) // message == "/storage/img1234.jpg"});

then image processor resizes...

Friday 9 November 2012

Page 18: Building businesspost.ie using Node.js

Node.js modules.

Friday 9 November 2012

Page 19: Building businesspost.ie using Node.js

Write small modules, not big apps!

Friday 9 November 2012

Page 20: Building businesspost.ie using Node.js

businesspost.ie modules‣underscore,‣connect,‣express,‣appgen,‣seneca

Friday 9 November 2012

Page 21: Building businesspost.ie using Node.js

‣npm install underscore‣underscorejs.org‣do functional programming‣provides "missing" JS features‣useful for handling arrays, objects

underscoremodule

Friday 9 November 2012

Page 22: Building businesspost.ie using Node.js

‣npm install connect‣senchalabs.org/connect‣build web services‣provides "middleware" pattern‣useful for HTTP request handling

connectmodule

Friday 9 November 2012

Page 23: Building businesspost.ie using Node.js

‣npm install express‣expressjs.com‣build websites‣provides HTML page handling‣useful for building dynamic sites

expressmodule

Friday 9 November 2012

Page 24: Building businesspost.ie using Node.js

‣npm install appgen‣appgenjs.org‣build multi-platform web apps‣generates multiple code versions‣useful for organizing platform code

appgenmodule

Friday 9 November 2012

Page 25: Building businesspost.ie using Node.js

‣npm install seneca‣senecajs.org‣build business logic‣provides "command" pattern‣useful for organizing features

senecamodule

Friday 9 November 2012

Page 26: Building businesspost.ie using Node.js

businesspost.ie lessons.

Friday 9 November 2012

Page 27: Building businesspost.ie using Node.js

Lesson:

Friday 9 November 2012

Page 28: Building businesspost.ie using Node.js

Lesson:

0

1

2

3

4

Client JavaScript Server JavaScript

code volume

Friday 9 November 2012

Page 29: Building businesspost.ie using Node.js

Lesson:multi-platform client-side JavaScript is really hard

• a framework is a must

• backbone.js

• business logic must be in common code

• browser-specific code

• virtual .js files

• use jshint to keep IE happy

• code against ECMA, use shims to support older browsers

• Code/Test/Debug inside Safari

• phonegap.github.com/weinre for hard to reach places

• use error capture in production

• Finally, use a simple static site as a fallback (also for Googlebot)

• appgen module helps!

Friday 9 November 2012

Page 30: Building businesspost.ie using Node.js

Lesson:multi-platform HTML/CSS is really hard

• "structured" CSS is a must

• sass or less

• Be happy with

•media queries

•CSS3 transforms

• browser-specific code

• virtual .css files

• Clean, semantic HTML is not optional

• graceful degradation may require radically different CSS

• 100% "Responsive" design is tough

• Responsive within browser subsets has higher reward/effort

•appgen module helps!

Friday 9 November 2012

Page 31: Building businesspost.ie using Node.js

Lesson:the app stores are not web sites

• that bug in version one...

•will take two weeks to fix via an update

• some users will never update

• appears after an OS update

• you can't deploy hot fixes

•make everything configurable!

•All prices, text, host names, urls, ...

•On launch, app "checks-in" for new configuration

• this will save your lifeFriday 9 November 2012

Page 32: Building businesspost.ie using Node.js

Lesson:Node.js is awesome!

• High performance

• High throughput

• Low CPU usage

• Constant memory usage

• leaks will kill, but then

• < 100ms startup time

• means you may not notice!

• callback spaghetti is not a problem in practice

•use functional style

• client-side code is far more difficult

•Don't do CPU intensive stuff

•Node.js is not for thatFriday 9 November 2012

Page 33: Building businesspost.ie using Node.js

Lesson:Small processes can save your life

• Prevents total failure even if other systems fail

• Easy to scale - just move to new machines

• Easy to code - you get small, single purpose code bases

• Easy to manage - even traditional process management will get you a long way

•Memory leaks are not a disaster - just restart!

•Use queues in preference to direct communication

•Easy to divide up work between developers

Friday 9 November 2012

Page 34: Building businesspost.ie using Node.js

Thank You!

Richard Rodger@rjrodger

Friday 9 November 2012