JS Everywhere

by Matteo Collina

This blink like it's 1998

what js is good for

if (el.style.visibility === 'hidden') {
  el.style.visibility = 'visible'
} else {
  el.style.visibility = 'hidden'
}

Flow control is built-in

If applications sends too much data, we can slow them down with backpressure!

function process(data, cb) {
  /* oh yeah, we are processing one
     thing at a time
     or we can do batches! */
  setTimeout(cb, 500)
}

Node.js has streams too!

source.pipe(dest).pipe(source)

Microservices

The best community ever

we love you too, npm

var HelloMessage = React.createClass({
  render: function() {
    return 
Hello {this.props.name}
; } }); ReactDOM.render(<HelloMessage name="John" />, mountNode);

NodeBots

Johnny Five

var five = require("johnny-five")
var board = new five.Board()

board.on('ready', function () {
  /* Create an Led on pin 13 */
  var led = new five.Led(13)
  /* Blink every half second */
  led.blink(500)
})

You should not see this!

Universal JS

An IoT system

Another IoT system

One more IoT system

The world is our playground..

..for now :)

What is an IoT system?


Architecture

Sensor Data Flow

Commands Flow

Problems

Using a pub/sub broker

Sensor Data Flow

Commands Flow

MQTT.js works on

MQTT

Publish-Subscribe


MQTT broker in Node.js

Mosca

Authentication in Mosca

var server = new mosca.Server(settings);
/* Accepts the connection if the username
   and password are valid */
function authClient(client, user, pass, cb) {
  var authorized = (
    user=== 'alice' &&
    pass.toString() == 'secret' );
  if (authorized) client.user = user;
  callback(null, authorized);
}
server.authenticate = authClient;

Publish Authorization in Mosca

var server = new mosca.Server(settings);
function authPub(client, topic, payload, cb) {
  var ok = client.user == topic.split('/')[1];
  /* we can alter the message here */
  if (ok) callback(null, payload);
  else callback(null, false);
}
server.authorizePublish = authPub;

Subscribe Authorization in Mosca

var server = new mosca.Server(settings);
function authSub(client, topic, cb) {
  var ok = client.user === topic.split('/')[1];
  cb(null, ok);
}
server.authorizeSubscribe = authSub;

Storing data in a timeseries

var server = new mosca.Server(settings);

function published(packet, client, callback) {
  timeseries.store(packet, callback);
}
server.published = published;

http://senecajs.org

var seneca = require('seneca')()
seneca.add({
  role:'user',
  cmd:'login'
}, function (args, callback) {
  var loggedIn = args.username === 'matteo' &&
                 args.password === 'collina'
  callback(null, { loggedIn:loggedIn })
})
seneca.listen()
var seneca = require('seneca')()
var client = seneca.client()
client.act({
  role:'user',
  cmd:'login',
  username: 'matteo',
  password: 'collina'
}, function (err, result) {
  console.log(result.loggedIn)
})


Features

Demo: an IoT system

Demo!

https://github.com/nearform/iot-system

Links

This presentation

Thanks!


If you need help with Node

or the Internet of Things


matteo.collina@nearform.com

@matteocollina on Twitter

www.nearform.com