Node.js Development in the
Cloud with Azure
Brian Benz, Sr. Technical Evangelist
Microsoft Open Technologies, Inc.
2 http://www.anglebrackets.org
Agenda
 Azure
 Node
 Node on Azure
 Web Sites
 IaaS/PaaS
 Examples
 Q&A
Azure
Flexible
Azure IaaS, PaaS, SaaS
Application Scenarios
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
Intro to Node.js
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
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.
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
Setting up Node.js
for Azure
12 http://www.anglebrackets.org
Download Node.js nodejs.org
13 http://www.anglebrackets.org
npm installer - www.npmjs.org
14 http://www.anglebrackets.org
Get the SDK - azure.microsoft.com/downloads
15 http://www.anglebrackets.org
Get the CLI - azure.microsoft.com/downloads
16 http://www.anglebrackets.org
Web Platform Installer – CLI and SDK
17 http://www.anglebrackets.org
Install Results
18 http://www.anglebrackets.org
Docs - azure.microsoft.com/documentation/
Node.js on Azure
Web Sites
20 http://www.anglebrackets.org
Node.js on Azure Web Sites
 Node Options in the Gallery
 Local Git Repository
21 http://www.anglebrackets.org
Node.js from the Web Site Gallery
22 http://www.anglebrackets.org
New > Web Site > Quick Create > Name and Click
23 http://www.anglebrackets.org
24 http://www.anglebrackets.org
Node.js from Local Git
25 http://www.anglebrackets.org
Get git - http://git-scm.com/downloads
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>
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.
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:homesiterepository'
to: 'D:homesitewwwroot'
remote: Deleting file: 'hostingstart.html'
remote: Copying file: 'server.js'
29 http://www.anglebrackets.org
Updating your Repository
 git add .
 git commit -m “commit message"
 git push azure master
 <Password>
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!
Node.js on Azure
IaaS/PaaS
Azure Virtual Machines – OS Options
Windows Server 2008 and 2012
OpenSUSE and SUSE Enterprise
CentOS
Ubuntu
Oracle Linux
Node at vmdepot.msopentech.com
azure topic verb options
Command Line Syntax Overview
prompt>
account
account location
account affinity-group
vm
vm disk
vm endpoint
vm image
service
service cert
site
config
download
import
list
show
delete
start
restart
shutdown
capture
create
attach
detach
browse
set
username
password
dns-prefix
vm-name
lb-port
target-image-name
source-path
disk-image-name
size-in-gb
thumbprint
value
-v
-vv
Automation Support
Capabilities
• Fully Customize VM with Data Disks and
Endpoint Configuration
• Automate Virtual Network Settings
Intro to Websocket
Deploying Web Sites on Windows
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
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”.
40 http://www.anglebrackets.org
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
Demo Architecture
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");});};
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();
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);
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');
}
48 http://www.anglebrackets.org
Broadcasting
var broadcast = function(sender, type, msg) {
socket.emit('message', {
sender: sender,
type: type,
data: msg
});
};
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 Azure
http://azure.microsoft.com/en-us/documentation/articles/web-sites-nodejs-develop-deploy-mac/
Q&A

Node.js on microsoft azure april 2014