Node.js on microsoft azure april 2014

50
Node.js Development in the Cloud with Azure Brian Benz, Sr. Technical Evangelist Microsoft Open Technologies, Inc.

description

Presented at DevIntesection / Anglebrackets 2014. In this session we showed how to code for the cloud with node.js and Windows Azure. We started with an introduction to the node.js Developer Center for Windows Azure, the Azure SDK for Java , and other tools and materials we’ve developed to make life easier for node developers. Included were lots of working examples showing tips and tricks for building, deploying, connecting, and maintaining node applications and multi-tier Web applications that access data, servers and services on Windows Azure.

Transcript of Node.js on microsoft azure april 2014

Page 1: Node.js on microsoft azure april 2014

Node.js Development in the

Cloud with Azure

Brian Benz, Sr. Technical Evangelist

Microsoft Open Technologies, Inc.

Page 2: Node.js on microsoft azure april 2014

2 http://www.anglebrackets.org

Agenda

Azure

Node

Node on Azure

Web Sites

IaaS/PaaS

Examples

Q&A

Page 3: Node.js on microsoft azure april 2014

Azure

Flexible

Page 4: Node.js on microsoft azure april 2014

Azure IaaS, PaaS, SaaS

Page 5: Node.js on microsoft azure april 2014

Application Scenarios

Page 6: Node.js on microsoft azure april 2014

6 http://www.anglebrackets.org

The Landscape - Node on Azure

Deployment

Web Sites

From Gallery

Via Source Deployment

IaaS / PaaS

Via Cloud Service deployment from Visual Studio

Deployment tools and source control: GitHub, TFS, FTP

Dev Tools

Azure Node SDK

Azure CLI

Microsoft Web Platform Installer

Microsoft Node Tools for Visual Studio

Page 7: Node.js on microsoft azure april 2014

Intro to Node.js

Page 8: Node.js on microsoft azure april 2014

8 http://www.anglebrackets.org

What is Node.js?

A JavaScript runtime environment

Server-side JavaScript

Compiled

Command line

JavaScript API for network and file system access

Designed for high concurrency

Events, not threads or processes

Never blocks, not even for I/O

Uses CommonJS framework syntax

Close to real OO

Page 9: Node.js on microsoft azure april 2014

9 http://www.anglebrackets.org

Why Use Node.js ?

JavaScript on server-side and client-side

Event loop with a stack

Node.JS serves requests in an event loop

Large number of simultaneous requests.

Avoids Context Switching

Waiting for I/O or Synchronization operation to complete.

Node attaches JS callbacks to I/O requests

The “event driven” part

When the original IO is completed, a callback to the server completes the request.

Page 10: Node.js on microsoft azure april 2014

10 http://www.anglebrackets.org

Speedy I/O With Events

Threads Events

Threads lock apps / threads One thread

Listens for incoming requests Process from queue

Multiple threads dependent on multiple processes

State saved for each event

Depends on context switching Not dependent on I/O or Synchronization

Page 11: Node.js on microsoft azure april 2014

Setting up Node.js for Azure

Page 12: Node.js on microsoft azure april 2014

12 http://www.anglebrackets.org

Download Node.js nodejs.org

Page 13: Node.js on microsoft azure april 2014

13 http://www.anglebrackets.org

npm installer - www.npmjs.org

Page 14: Node.js on microsoft azure april 2014

14 http://www.anglebrackets.org

Get the SDK - azure.microsoft.com/downloads

Page 15: Node.js on microsoft azure april 2014

15 http://www.anglebrackets.org

Get the CLI - azure.microsoft.com/downloads

Page 16: Node.js on microsoft azure april 2014

16 http://www.anglebrackets.org

Web Platform Installer – CLI and SDK

Page 17: Node.js on microsoft azure april 2014

17 http://www.anglebrackets.org

Install Results

Page 18: Node.js on microsoft azure april 2014

18 http://www.anglebrackets.org

Docs - azure.microsoft.com/documentation/

Page 19: Node.js on microsoft azure april 2014

Node.js on Azure Web Sites

Page 20: Node.js on microsoft azure april 2014

20 http://www.anglebrackets.org

Node.js on Azure Web Sites

Node Options in the Gallery

Local Git Repository

Page 21: Node.js on microsoft azure april 2014

21 http://www.anglebrackets.org

Node.js from the Web Site Gallery

Page 22: Node.js on microsoft azure april 2014

22 http://www.anglebrackets.org

New > Web Site > Quick Create > Name and Click

Page 23: Node.js on microsoft azure april 2014

23 http://www.anglebrackets.org

Page 24: Node.js on microsoft azure april 2014

24 http://www.anglebrackets.org

Node.js from Local Git

Page 25: Node.js on microsoft azure april 2014

25 http://www.anglebrackets.org

Get git - http://git-scm.com/downloads

Page 26: Node.js on microsoft azure april 2014

26 http://www.anglebrackets.org

Setting up your Repository

git init

git add .

git commit -m “first commit"

git remote add azure <URL created >

git push azure master

<Password>

Page 27: Node.js on microsoft azure april 2014

27 http://www.anglebrackets.org

Initial Deployment

Counting objects: 3, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (3/3), 408 bytes | 0 bytes/s,

done.

Total 3 (delta 0), reused 0 (delta 0)

remote: Updating branch 'master'.

remote: Updating submodules.

Page 28: Node.js on microsoft azure april 2014

28 http://www.anglebrackets.org

Submodules

remote: Preparing deployment for commit id

'8f69499edd'.

remote: Generating deployment script.

remote: Generating deployment script for node.js Web

Site

remote: Generated deployment script files

remote: Running deployment command...

remote: Handling node.js deployment.

remote: KuduSync.NET from: 'D:\home\site\repository'

to: 'D:\home\site\wwwroot'

remote: Deleting file: 'hostingstart.html'

remote: Copying file: 'server.js'

Page 29: Node.js on microsoft azure april 2014

29 http://www.anglebrackets.org

Updating your Repository

git add .

git commit -m “commit message"

git push azure master

<Password>

Page 30: Node.js on microsoft azure april 2014

30 http://www.anglebrackets.org

Node.js Tools for Visual Studio 1.0

VS 2013, 2013, VS Express 2013

Project Templates

Web Sites, Apps, Worker Roles, VMs/Servers

Typeahead

Dependency Management

Publishing

Using Publishing credentials

Deployment via Git

Remote Debugging

Even on Linux!

Page 31: Node.js on microsoft azure april 2014

Node.js on Azure IaaS/PaaS

Page 32: Node.js on microsoft azure april 2014

Azure Virtual Machines – OS Options

Windows Server 2008 and 2012

OpenSUSE and SUSE Enterprise

CentOS

Ubuntu

Oracle Linux

Page 33: Node.js on microsoft azure april 2014

Node at vmdepot.msopentech.com

Page 34: Node.js on microsoft azure april 2014

azure topic verb options

Command Line Syntax Overview

prompt>

accountaccount locationaccount affinity-groupvmvm diskvm endpointvm imageserviceservice certsiteconfig

downloadimportlistshowdeletestartrestartshutdowncapturecreateattachdetachbrowseset

usernamepassworddns-prefixvm-namelb-porttarget-image-namesource-pathdisk-image-namesize-in-gbthumbprintvalue-v-vv

Page 35: Node.js on microsoft azure april 2014

Automation Support

Capabilities

• Fully Customize VM with Data Disks and Endpoint Configuration

• Automate Virtual Network Settings

Page 36: Node.js on microsoft azure april 2014

Intro to WebsocketDeploying Web Sites on Windows

Page 37: Node.js on microsoft azure april 2014

37 http://www.anglebrackets.org

WebSocket

Two-way browser communication

HTTP/1.1 upgrade header for communication

Keeps connections “open” after sending or receiving messages

UTF-8 encoding (no binary)

Just a transport – Challenges:

Server needs to perform a handshake based on the WebSocket protocol, and implement security separately

WebSocket not supported by all browsers/versions

Page 38: Node.js on microsoft azure april 2014

38 http://www.anglebrackets.org

Node.js with Socket.io

Node.js - Scalable, Event-Driven HTTP server

JavaScript and simple API

Socket.io on Node.js

Socket.io adds the WebSocket protocol and server-side security to Node.js

HTML5 browsers can receive WebSocket connections and keep them “open”.

Page 39: Node.js on microsoft azure april 2014
Page 40: Node.js on microsoft azure april 2014

40 http://www.anglebrackets.org

Page 41: Node.js on microsoft azure april 2014

41 http://www.anglebrackets.org

Conference Scheduler

Attendees can track sessions, vote sessions up and down

Offline Capabilities

IndexedDB

CouchDB VM for Data Storage

Web site hosting the application

Node.js

Grunt

Page 42: Node.js on microsoft azure april 2014

Demo Architecture

Page 43: Node.js on microsoft azure april 2014

43 http://www.anglebrackets.org

Local or Server – CouchDb or PouchDB

app.replicate = function(callback, type) {

Pouch.replicate(CONF.local.userprefs,

CONF.remote.userprefs, function() {callback("User

Perferences");});

Pouch.replicate(CONF.remote.sessions,

CONF.local.sessions, function() {callback("Session

Data");});};

Page 44: Node.js on microsoft azure april 2014

44 http://www.anglebrackets.org

Get Sessions from CouchDB

app.loadSessions = function() {

$("#loader").modal("show");

$.getScript("sessions.js?" +

Math.random()).then(function(data) {

$("#loader").modal("hide");_.each(CONF.sessions,

function(s) {var models = app.sessionList.where({

name: s.name});if(models.length === 0) {model = new

app.Model.Session();} else {model = models[0];}

model.set(s);model.server = CONF.local.sessions;

model.save();app.sessionList.add(model);});

app.sessionList.fetch();

Page 45: Node.js on microsoft azure april 2014
Page 46: Node.js on microsoft azure april 2014

46 http://www.anglebrackets.org

Handling connections

var socket = io.connect(document.location.protocol +

'//' + document.location.host + '/');

var connected = false;

socket.on('connected', function(data) {

connected = true;

});

socket.on('message', function(data) {

//console.log('Message', data);

if (typeof processMessage[data.sender] === 'object')

{

if (typeof processMessage[data.sender][data.type]

=== 'function') {

processMessage[data.sender][data.type](data);

Page 47: Node.js on microsoft azure april 2014

47 http://www.anglebrackets.org

Notifications

Function addAudience(data, isPresenter) {

data = data || {};

data.img = data.img || ('/img/avatar' + (1 +

parseInt(Math.random() * 10 % 3)) + '.png');

data.data = data.data || {};

$('#audienceList').append($('<img>', {

src: data.img,

class: (isPresenter ? 'presenter' : ''),

title: data.data.name

}).data('id', data.id));

notify((data.data.name || 'Someone') + ' joined the

meeting');

}

Page 48: Node.js on microsoft azure april 2014

48 http://www.anglebrackets.org

Broadcasting

var broadcast = function(sender, type, msg) {

socket.emit('message', {

sender: sender,

type: type,

data: msg

});

};

Page 49: Node.js on microsoft azure april 2014

49 http://www.anglebrackets.org

Where to learn more

Node.js

http://nodejs.org

Node.js Dev Center

http://azure.microsoft.com/en-us/develop/nodejs/

Node Tools for Visual Studio

https://nodejstools.codeplex.com/

Code Samples Today:

https://github.com/axemclion

Tutorial: Building a multi-tier cloud application on Windows Azure using MongoLab, node.js, and Socket.IO

http://msopentech.com/blog/2013/09/18/tutorial-building-multi-tier-cloud-application-windows-azure-using-mongolab-node-js-socket-io/

Tutorial – Building a Multi-Tier Windows Azure Web application use Cloudant’s Couchdb-as-a-Service, node.js, CORS, and Grunt

http://msopentech.com/blog/2013/12/19/tutorial-building-multi-tier-windows-azure-web-application-use-cloudants-couchdb-service-node-js-cors-grunt-2/

Tutorial: Build and deploy a Node.js web site to Azurehttp://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/

Page 50: Node.js on microsoft azure april 2014

Q&A