AZ02 – NODE.JS SU WINDOWS AZURE


Emanuele DelBono
Software engineer
@emadb


                    #CDays13 – 27 e 28 febbraio 2013
grazie a…
            Sponsor
Do you really know
    javascript?
> [] + []
> [] + []
''
> [] + {}
> [] + {}
{}
> {} + []
> {} + []
0
> {} + {}
> {} + {}
NaN
> Array(10)
> Array(10)
[ , , , , , , , , , , ]
> Array(10)
[ , , , , , , , , , , ]
> Array(10).join('yo')
> Array(10)
[ , , , , , , , , , , ]
> Array(10).join('yo')
yoyoyoyoyoyoyoyoyoyoyo
> Array(10)
[ , , , , , , , , , , ]
> Array(10).join('yo')
yoyoyoyoyoyoyoyoyoyoyo
> Array(10).join('yo' + 1)
> Array(10)
[ , , , , , , , , , , ]
> Array(10).join('yo')
yoyoyoyoyoyoyoyoyoyoyo
> Array(10).join('yo' + 1)
yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo
> Array(10)
[ , , , , , , , , , , ]
> Array(10).join('yo')
yoyoyoyoyoyoyoyoyoyoyo
> Array(10).join('yo' + 1)
yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo
> Array(10).join('yo' - 1) + ' Batmaaan'
> Array(10)
[ , , , , , , , , , , ]
> Array(10).join('yo')
yoyoyoyoyoyoyoyoyoyoyo
> Array(10).join('yo' + 1)
yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo
> Array(10).join('yo' - 1) + ' Batmaaan'

NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batmaaan
Agenda
•   What is node.js?
•   Architecture
•   Installation
•   Building applications
•   Modules
I am…
    Software engineer and
•
    Architect in codiceplastico.
    Writes web apps in C#,
    javascript e ruby.
What is node.js?
• A  development  framework  that  uses  
  event  driven,  non  blocking  I/O
• Built  on  javascript
• Based  on  Google  V8  engine
• Ideal  for  high  performance
History
• Created by Ryan Dahl in 2009
• He is trying to find the best way
  to notify the user in real time
• Written in C
• https://github.com/joyent/node
Curious facts
• It’s one of the most watched project on
  Github
• The community has build more than 21k
  modules
• It’s not only for web apps
Web apps scalability
• Synchronous:
  – Every request could block the others
• Multithread
  – Hard times with thousands connections
Node keep it simple


  Single thread
Non-blocking
• Blocking
 var result = db.query(‘select ...’)


• Non blocking
 db.query(‘select...’,function(result) {...})
Why didn’t I think at it before?
• Culture: The non blocking code seems
  more difficult to get
• Infrastructure: single threaded event loop
  require non-blocking I/O
The event loop
• Every operation must be non-
  blocking
• Lots of callbacks
• The result will be available in
  one of the next ticks
The event loop
• No code in the main method
• All I/O operations are asynchronous
• Be quick to respond
YOU HAVE TO THINK IN
    CALLBACKS
Javascript
• Seems the best choice:
  – Anonymous functions, closures
  – One callback at a time
  – None were using it on the server side
Who are using it?




https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node
What can you do with node?
            Web Server
            TCP Server
          Robot controller
         Command line apps
            Proxy server
          Streaming server
          VoiceMail server
           Music machine
Install
What you get?
• node executable
• npm (the node packet manager)
• REPL
DEMO
IDE
• WebMatrix on windows
• Cloud9
• WebStorm

• Don’t forget about the command line + text
  editor
Not only for web apps
• Useful for I/O intensive tasks
• Scripts for DevOps
• Build (jake)

• Not suitable for intensive CPU tasks
Modules (npm)
• NPM is the Nuget for noders
• No version clash
• package.json to manage app
  dependencies
DEMO
Express.js
•   Minimal MVC framework
•   Verb oriented
•   Middleware
•   Templating
Express.js
var express = require('express');
var app = express();

app.get('/', function(req, res){
  res.send('hello noders');
});

app.listen(3000);
Jade
doctype 5
html(lang="en")
  head
    title= pageTitle
    script(type='text/javascript')
       if (foo) {
          bar()
       }
  body
    h1 Jade - node template engine
    #container
       if youAreUsingJade
         p You are amazing
       else
         p Get on it!
DEMO
Azure
• You can deploy your app to Windows
  Azure.
• Using the CLI
• From WebMatrix (for IDE addicted)
Node on Azure
• Web sites
• Cloud services
• Virtual machines
Deploy on azure
$ azure site create nodeisfun --git
$ git add .
$ git commit -m’first commit’
$ git push azure master
$ azure site browse
DEMO
PACKAGES
mongodb
var MongoClient = require('mongodb').MongoClient;

MongoClient.connect("mongodb://localhost:27017/myDb",
function(err, db) {

  var collection = db.collection('myCollection');

  collection.findOne({mykey:1}, function(err, item){
    console.log(item)
  });
});
node-sqlserver
conn.queryRaw("SELECT * FROM Employees", function (err, res) {
  if (err) {
   console.log("Error running query!");
    return;
  }
  for (var i = 0; i < res.rows.length; i++) {
   console.log(res.rows[i][0] + " " + res.rows[i][1]);
  }
});
WebSockets
• Full-duplex communication over TCP
• W3C standard

• The server calls the browser!!
WebSockets: socket.io
var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});
WebSockets: socket.io
<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
DEMO
• A collaborative Drum Machine
  – Node.js/express
  – Azure
  – WebSockets
  – Web Audio API
LET’S DRUM
Conclusions
• Node is an interesting platform
• It worth a look
• It’s becoming popular
Not all problems
should be solved in C#
Q&A
Slides and demos available here
  http://www.communitydays.it/
    http://github.com/emadb/
   http://slideshare.net/emadb/

Node azure

  • 1.
    AZ02 – NODE.JSSU WINDOWS AZURE Emanuele DelBono Software engineer @emadb #CDays13 – 27 e 28 febbraio 2013
  • 2.
    grazie a… Sponsor
  • 3.
    Do you reallyknow javascript?
  • 5.
  • 6.
    > [] +[] ''
  • 8.
  • 9.
    > [] +{} {}
  • 11.
  • 12.
    > {} +[] 0
  • 14.
  • 15.
    > {} +{} NaN
  • 17.
  • 18.
    > Array(10) [ ,, , , , , , , , , ]
  • 19.
    > Array(10) [ ,, , , , , , , , , ] > Array(10).join('yo')
  • 20.
    > Array(10) [ ,, , , , , , , , , ] > Array(10).join('yo') yoyoyoyoyoyoyoyoyoyoyo
  • 21.
    > Array(10) [ ,, , , , , , , , , ] > Array(10).join('yo') yoyoyoyoyoyoyoyoyoyoyo > Array(10).join('yo' + 1)
  • 22.
    > Array(10) [ ,, , , , , , , , , ] > Array(10).join('yo') yoyoyoyoyoyoyoyoyoyoyo > Array(10).join('yo' + 1) yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo
  • 23.
    > Array(10) [ ,, , , , , , , , , ] > Array(10).join('yo') yoyoyoyoyoyoyoyoyoyoyo > Array(10).join('yo' + 1) yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo > Array(10).join('yo' - 1) + ' Batmaaan'
  • 24.
    > Array(10) [ ,, , , , , , , , , ] > Array(10).join('yo') yoyoyoyoyoyoyoyoyoyoyo > Array(10).join('yo' + 1) yo1yo1yo1yo1yo1yo1yo1yo1yo1yo1yo > Array(10).join('yo' - 1) + ' Batmaaan' NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN Batmaaan
  • 25.
    Agenda • What is node.js? • Architecture • Installation • Building applications • Modules
  • 26.
    I am… Software engineer and • Architect in codiceplastico. Writes web apps in C#, javascript e ruby.
  • 27.
    What is node.js? •A  development  framework  that  uses   event  driven,  non  blocking  I/O • Built  on  javascript • Based  on  Google  V8  engine • Ideal  for  high  performance
  • 28.
    History • Created byRyan Dahl in 2009 • He is trying to find the best way to notify the user in real time • Written in C • https://github.com/joyent/node
  • 29.
    Curious facts • It’sone of the most watched project on Github • The community has build more than 21k modules • It’s not only for web apps
  • 30.
    Web apps scalability •Synchronous: – Every request could block the others • Multithread – Hard times with thousands connections
  • 31.
    Node keep itsimple Single thread
  • 33.
    Non-blocking • Blocking varresult = db.query(‘select ...’) • Non blocking db.query(‘select...’,function(result) {...})
  • 34.
    Why didn’t Ithink at it before? • Culture: The non blocking code seems more difficult to get • Infrastructure: single threaded event loop require non-blocking I/O
  • 35.
    The event loop •Every operation must be non- blocking • Lots of callbacks • The result will be available in one of the next ticks
  • 36.
    The event loop •No code in the main method • All I/O operations are asynchronous • Be quick to respond
  • 37.
    YOU HAVE TOTHINK IN CALLBACKS
  • 38.
    Javascript • Seems thebest choice: – Anonymous functions, closures – One callback at a time – None were using it on the server side
  • 39.
    Who are usingit? https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node
  • 40.
    What can youdo with node? Web Server TCP Server Robot controller Command line apps Proxy server Streaming server VoiceMail server Music machine
  • 41.
  • 42.
    What you get? •node executable • npm (the node packet manager) • REPL
  • 43.
  • 44.
    IDE • WebMatrix onwindows • Cloud9 • WebStorm • Don’t forget about the command line + text editor
  • 45.
    Not only forweb apps • Useful for I/O intensive tasks • Scripts for DevOps • Build (jake) • Not suitable for intensive CPU tasks
  • 46.
    Modules (npm) • NPMis the Nuget for noders • No version clash • package.json to manage app dependencies
  • 47.
  • 48.
    Express.js • Minimal MVC framework • Verb oriented • Middleware • Templating
  • 49.
    Express.js var express =require('express'); var app = express(); app.get('/', function(req, res){ res.send('hello noders'); }); app.listen(3000);
  • 50.
    Jade doctype 5 html(lang="en") head title= pageTitle script(type='text/javascript') if (foo) { bar() } body h1 Jade - node template engine #container if youAreUsingJade p You are amazing else p Get on it!
  • 51.
  • 52.
    Azure • You candeploy your app to Windows Azure. • Using the CLI • From WebMatrix (for IDE addicted)
  • 53.
    Node on Azure •Web sites • Cloud services • Virtual machines
  • 54.
    Deploy on azure $azure site create nodeisfun --git $ git add . $ git commit -m’first commit’ $ git push azure master $ azure site browse
  • 55.
  • 56.
  • 57.
    mongodb var MongoClient =require('mongodb').MongoClient; MongoClient.connect("mongodb://localhost:27017/myDb", function(err, db) { var collection = db.collection('myCollection'); collection.findOne({mykey:1}, function(err, item){ console.log(item) }); });
  • 58.
    node-sqlserver conn.queryRaw("SELECT * FROMEmployees", function (err, res) { if (err) {    console.log("Error running query!");     return;   }   for (var i = 0; i < res.rows.length; i++) {    console.log(res.rows[i][0] + " " + res.rows[i][1]);   } });
  • 59.
    WebSockets • Full-duplex communicationover TCP • W3C standard • The server calls the browser!!
  • 60.
    WebSockets: socket.io var io= require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); });
  • 61.
    WebSockets: socket.io <script src="/socket.io/socket.io.js"></script> <script> var socket = io.connect('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); }); </script>
  • 62.
    DEMO • A collaborativeDrum Machine – Node.js/express – Azure – WebSockets – Web Audio API
  • 63.
  • 64.
    Conclusions • Node isan interesting platform • It worth a look • It’s becoming popular
  • 65.
    Not all problems shouldbe solved in C#
  • 66.
    Q&A Slides and demosavailable here http://www.communitydays.it/ http://github.com/emadb/ http://slideshare.net/emadb/