node and azure

62
1 Node.js and Azure 1 Building Applications for the Web

Upload: jasongerard

Post on 28-Jan-2015

129 views

Category:

Technology


6 download

DESCRIPTION

Node.js and Azure presentation for Code on the Beach August 2013

TRANSCRIPT

Page 1: Node and Azure

1

Node.js and Azure

1

Building Applications for the Web

Page 2: Node and Azure

2

Jason Gerard

•Director of Product Development, PaySpan Inc. (www.payspan.com)

•Developing software for 15 years (VB, COM, JAVA, .NET)

• Focus on SaaS

•Married with 3 kids

Page 3: Node and Azure

3

What is node.js

•Created by Ryan Dahl in 2009

• Platform for asynchronous JavaScript

•Event based programming• Event loop fundamental structure

•DIRTy• Data Intensive Real Time

•Solution for c10k problem

Page 4: Node and Azure

4

Architecture

•Written in C++ and JavaScript

• 20k lines of C++, 2k lines of JavaScript

• Built with Chrome’s V8 JavaScript Engine

• Libev

• Native Event Loop

• Libeio

• uses POSIX threads to provide async IO

• Evcom

• Stream socket library built on libev

• udns

Page 5: Node and Azure

5

Events vs. Threads

• Threads• Each request gets its own thread

• Thread can’t process another request until it’s done with current

• 2 MB stack per thread

• Context Switching

• Events• Each request is added to a queue

• Processed by single thread

• Only one thread, no context switching

5

Page 6: Node and Azure

6

Why JavaScript?

•Ubiquitous

• Everyone knows it

• “WORA”

• Stole Java’s name and beat it at it’s own game

• Front end developers can easily transition to server development

• Already used to event based programming

Page 7: Node and Azure

7

ASYNCHRONOUS JAVASCRIPT

• Same programming model as web browser

• Wait for events

• Non Blocking IO

• Uses Callbacks

7

Page 8: Node and Azure

8

Callbacks

• “Everything” is a callback

• Only one thread, don’t block it

• Building Blocks of Async Programming

8

Page 9: Node and Azure

9

Consuming Events

• Example with sockets

9

Page 10: Node and Azure

10

Event Emitters

• EventEmitter class

• emitter.addListener(event, listener)

• emitter.on(event, listener)

• emitter.once(event, listener)

• emitter.removeListener(event, listener)

• emitter.removeAllListeners([event])

• emitter.setMaxListeners(n)

• emitter.listeners(event)

• emitter.emit(event, [arg1], [arg2], [...])

Page 11: Node and Azure

11

Event Example

11

Page 12: Node and Azure

12

Nodemon

• Monitors for file changes

• Automatically restarts node process

• nodemon app.js

12

Page 13: Node and Azure

13

Package Management

• npm

• command for creating, installing, updating packages

• npm test (runs test scripts)

• package.json

• JSON file describing package

13

Page 14: Node and Azure

14

npm

• npm install

• npm install module_name

• npm install -g module_name (don’t forget to sudo)

14

Page 15: Node and Azure

15

package.json

15

Page 16: Node and Azure

16

Web Development

• Node’s bread and butter

• Basic HTTP server right in the box

16

Page 17: Node and Azure

17

Connect

• Extends HTTP support

• Adds concept of “middleware”

• function (req, res, next){//do somethingnext();}

• Configured with use() method

• Order IS important

17

Page 18: Node and Azure

18

Middleware Example

18

Page 19: Node and Azure

19

Express

• Framework built on top of connect

• Provides view engine facilities

• Routes

• Many helper functions

19

Page 20: Node and Azure

20

Express Routing

• Inspired by Sinatra (ruby)

• /users

• /users/:id

• regex

20

Page 21: Node and Azure

21

Express Routing

• app.VERB() where verb is an HTTP verb (get, post, etc...)

• app.all() execute for all verbs

• req.query for querystring parameters

• req.body for POST, must use bodyParser middleware

21

Page 22: Node and Azure

22

Error Handling

• Basic middleware

• function handler(err, req, res, next)

22

Page 23: Node and Azure

23

View Engines

• EJS

• Jade

• Mustache

• Hogan

• Dust

• Swig

• Flatiron Plates

23

Page 24: Node and Azure

24

EJS

24

Page 25: Node and Azure

25

Jade

25

Page 26: Node and Azure

26

Mustache

26

Page 27: Node and Azure

27

Mustache Output

27

Page 28: Node and Azure

28

Mustache derivatives

• Hogan (builded with express, built by twitter)

• Handlebars

28

Page 29: Node and Azure

29

Flatiron Plates

• Created by nodejitsu

• Pure HTML binding

29

Page 30: Node and Azure

30

Consolidate

• Provides support for many view engines

• Exposes each view engine as Express middleware

• Wraps each view invocation in function

• function x(path[, locals], callback)

• Same format Express uses

30

Page 31: Node and Azure

31

Consolidate supported Template Engines

• atpl

• dust

• eco

• ejs

• haml

• haml-coffee

• handlebars

• hogan

• jade

• jazz

31

• jqtpl

• JUST

• liquor

• mustache

• QEJS

• swig

• templayed

• toffee

• underscore

• walrus

• whiskers

Page 32: Node and Azure

32

Using Consolidate

• Assign file extension to viewengine

• specify views folder

• render() instead of write()

32

Page 33: Node and Azure

33

Socket IO

• Realtime web apps

• Broad browser support• Internet Explorer 5.5+

• Safari 3+

• Google Chrome 4+

• Firefox 3+

• Opera 10.61+

• iPhone Safari

• iPad Safari

• Android WebKit

• WebOs WebKit

33

• Falls back to next best transport• WebSocket• Adobe® Flash® Socket• AJAX long polling• AJAX multipart streaming• Forever Iframe• JSONP Polling

Page 34: Node and Azure

34

Socket.IO vs SignalR

• Socket.IO

• Broader browser support

• More fallback transports

• JavaScript for client and Server

34

• SignalR

• Less Browser support

• Less transports

• .NET code on server

Page 35: Node and Azure

35

Data Access

• Drivers exists for most databases

• Mongo

• CouchDB

• SQL Server

• MySQL

• PostgreSQL

• unixODBC

35

Page 36: Node and Azure

36

SQL Server

• node-sqlserver

• Microsoft Provided module

• https://github.com/WindowsAzure/node-sqlserver

• edge.js

• execute SQL using the .NET SQL Client

• supports CRUD operations

36

Page 37: Node and Azure

37

SQL DRIVER

• node-sqlserver

• Developed by Microsoft

• Not a lot of activity on GitHub. (38 open issues)

• Not production ready

37

Page 38: Node and Azure

38

SQL with EDGE.JS

38

Page 39: Node and Azure

39

Other options (SQL Server)

• node-tds

• TDS implementation for node (Pure JS)

• tedious

• TDS implementation for node

• odbc

• Good ole’ ODBC

• uses unixODBC library

• Windows Support?

39

Page 40: Node and Azure

40

Deployment Models

• node process

• nginx

• iisnode

• cloud

40

Page 41: Node and Azure

41

Node Process Direct

• Simplest option

• Doesn’t reload changed files

• requires restart

• Not a service out of the box on windows

• Easily fixed (winser npm module)

• Not good at serving static content

• Good for development, though nodemon is better choice41

Page 42: Node and Azure

42

nginx

• Extremely fast HTTP server

• Great for static content

• Proxy Dynamic Calls to Node

• Much faster than Apache

• Poor windows support

• current version considered beta

• “high performance and scalability should not be expected”

42

Page 43: Node and Azure

43

iisnode

• Created by Tomas Janczuk

• Native IIS module

• Powers Node on Windows Azure

• Windows only (if that’s not obvious you’re at the wrong conference)

43

Page 44: Node and Azure

44

Configuration

44

Page 45: Node and Azure

45

performance

• Great when serving static content with application

• Large overhead for pure dynamic app

45

Page 46: Node and Azure

46

Cloud

• Heroku

• Nodejitsu

• Engine Yard

• Windows Azure

• Longer list at https://github.com/joyent/node/wiki/Node-Hosting

46

Page 47: Node and Azure

47

Windows Azure

• MS Deploy

• Git deploy

• Same config as iisnode

47

Page 48: Node and Azure

48

Node azure sdk

• npm install azure-cli -g

• Windows, linux, OS X

48

Page 49: Node and Azure

49

SDK Features• Tables

• create and delete tables

• create, query, insert, update, merge, and delete entities

• Blobs

• create, list, and delete containers, work with container metadata and permissions, list blobs in container

• create block and page blobs (from a stream, a file, or a string), work with blob blocks and pages, delete blobs

• work with blob properties, metadata, leases, snapshot a blob

• HD Insight

• create, list and delete HDInsight clusters

49

Page 50: Node and Azure

50

SDK Features Continued• Storage Queues

• create, list, and delete queues, and work with queue metadata

• create, get, peek, update, delete messages

• Service Bus

• Queues: create, list and delete queues; create, list, and delete subscriptions; send, receive, unlock and delete messages

• Topics: create, list, and delete topics; create, list, and delete rules

• Notification hubs: create hubs, register for messages, send messages

• Azure SQL Database

• create, list and delete Azure SQL Database servers, databases and firewall rules

• Service Runtime

• discover addresses and ports for the endpoints of other role instances in your service

• get configuration settings and access local resources

• get role instance information for current role and other role instances

• query and set the status of the current role

50

Page 51: Node and Azure

51

Deploying to azure

npm install azuregit commit -m "My first Node app"git push azure master

•IT’S THAT EASY!

51

Page 52: Node and Azure

52

Azure SDK Example

52

Page 53: Node and Azure

53

Using .NET with Node

• Edge.js

• Also created by Tomas Janczuk

• Supported on Azure

• http://tjanczuk.github.io/edge/#/

53

Page 54: Node and Azure

54

Edge.js

54

Page 55: Node and Azure

55

Edge.js

55

Page 56: Node and Azure

56

Resources

• Learning Node

• Books

• Node.js in Action (Manning)

• Professional Node.js (Wrox)

• Pro Node.js (Apress)

• Node (O’reilly)

• WebSites

• nodejs.org

• howtonode.org

• codeschool.com

• education.10gen.com

56

LinkedIn - HTML5 Lessons from LinkedIn using Node.js by Kiran Prasadhttps://www.youtube.com/watch?v=hMd45Ij2DYQ

linkedin.com/techtalks

Page 57: Node and Azure

57

Webstorm

• Based onIntelliJ IDEA

• Great if you likeReSharper

• Windows, OS XLinux

57

Page 58: Node and Azure

58

webmatrix

• IntelliSense

• Support for Jade and EJS

• LESS and Sass support

• Coffeescript support

58

Page 59: Node and Azure

59

Visual Studio

• Node.js tools for Visual Studio

• Node projects, debugger, npm console

• Syntax highlighting

• Visual Node

• Node projects, debugger, packages as references, REPL

• IntelliSense, Syntax highlighting

• Created by redgate

• Private beta (http://www.visualnode.info/)59

Page 60: Node and Azure

60

Cloud9 IDE

• Built with node ;-)

• Runs in the browser

• jsfiddle for node

60

Page 61: Node and Azure

61

FInal Thoughts

• Node is for IO intensive applications

• Web Applications

• 1000s of concurrent connections

• NOT for Compute bound operations

• Don’t try to calculate PI and expect a responsive server

• Fast development, easy deployment

• No more edit, build, wait, refresh -- just edit and refresh!

61

Page 62: Node and Azure

62

Questions?

62