58
@matteocollina matteocollina.com Node.js Conference - Brescia 2012/11/09 Building a multi-protocol broker for the Internet of Things using node.js

Building a multi protocol broker for the internet of things using nodejs

Embed Size (px)

DESCRIPTION

Have you ever wondered how to interconnect your apps with physical things? Have you ever felt that the request/response pattern of HTTP is not enough? What about a binary protocol? In this talk you will discover the internal of the open source QEST broker, a Node.js-based broker for the Internet of Things that implements a classic publish/subscribe pattern, while making it accessible from HTTP and MQTT, an ultra-fast binary protocol.

Citation preview

Page 1: Building a multi protocol broker for the internet of things using nodejs

@matteocollinamatteocollina.com

Node.js Conference - Brescia 2012/11/09

Building a multi-protocol broker for the Internet of Things using node.js

Page 2: Building a multi protocol broker for the internet of things using nodejs

How many people will own a smartphone by 2014

1.000.000.000

Page 3: Building a multi protocol broker for the internet of things using nodejs

Did you see it coming

http://www.flickr.com/photos/12738000@N00/360231193/

Page 4: Building a multi protocol broker for the internet of things using nodejs

...They didn’t!

Page 5: Building a multi protocol broker for the internet of things using nodejs

What's next(hint: it's a big number)

50.000.000.000

Page 6: Building a multi protocol broker for the internet of things using nodejs

1.000.000.000

50.000.000.000

Page 7: Building a multi protocol broker for the internet of things using nodejs

"Things"

Page 8: Building a multi protocol broker for the internet of things using nodejs

Let's experiment

Page 9: Building a multi protocol broker for the internet of things using nodejs

I want to chat with my house

Goal:

Page 10: Building a multi protocol broker for the internet of things using nodejs

>: hi househi matteo

>: what's the temperature?20

Page 11: Building a multi protocol broker for the internet of things using nodejs

>: 4 8 15 16 23 42

If you don’t understand this, you are not a geek!

Page 12: Building a multi protocol broker for the internet of things using nodejs

Hubot © 2012 GitHub Inc. All rights

Enter Hubot

Page 13: Building a multi protocol broker for the internet of things using nodejs

Hubot © 2012 GitHub Inc. All rights

A programmable robotthat is controlled through chat

Page 14: Building a multi protocol broker for the internet of things using nodejs

We can supercharge our house with hubot

Page 15: Building a multi protocol broker for the internet of things using nodejs

module.exports = (robot) ->

robot.respond /what’s the temperature?/i, (msg) -> msg.http("http://mchouse.it/temperature") .header("Accept", "application/json") .get() (err, res, body) -> msg.send JSON.parse(body)

We can supercharge our house with hubot

How

Page 16: Building a multi protocol broker for the internet of things using nodejs

In order to ask our temp to hubot, we need to:

1. sense the temp2. get the temp on the web3. build a web API

Page 17: Building a multi protocol broker for the internet of things using nodejs

We are building an API

In Italy,“API” means

“bees”http://www.flickr.com/photos/theseanster93/4056815767

Page 18: Building a multi protocol broker for the internet of things using nodejs

What do we need to sense my house temperature

Page 19: Building a multi protocol broker for the internet of things using nodejs

TMP36

TMP36

We add a sensor to an Arduino

Page 20: Building a multi protocol broker for the internet of things using nodejs

TMP36

We add an ethernet shield to connect to the Internet

http://www.flickr.com/photos/snootlab/6052465980/

Page 21: Building a multi protocol broker for the internet of things using nodejs

What protocol do we use to push our temperature to our API?

Page 23: Building a multi protocol broker for the internet of things using nodejs

We need a fast, binary protocol

http://www.flickr.com/photos/grrphoto/305649629

Page 24: Building a multi protocol broker for the internet of things using nodejs

M2M protocols are mostly ad-hoc, and researchers and businesses focus on low level problems.

“Things” should interact with our lives, and all the

technology should be built to make them easy

to use.

Page 25: Building a multi protocol broker for the internet of things using nodejs

• “things” exposed with binary protocol

• publish/subscribe

• topics as the naming system

• “things” exposed to the web

• request/response

• URIs as the naming system

Page 27: Building a multi protocol broker for the internet of things using nodejs

• MQTT broker

• REST interface

• HTTP semantics

• no QoS

• built on node.js and redis

QESTREST Server

Redis

MQTT Server

QEST

Data Layer

HTTP Clients MQTT Clients

Page 28: Building a multi protocol broker for the internet of things using nodejs

Web App

DeviceSC

LS

DA

AR

EF

GN

D

IOR

EF

RE

SE

T3V

3P

WM

PW

M

PW

M

L

TXRX ON

ICSP

PW

MP

WM

PW

M TX

RX

31

21

11

01

9 8DIGITAL

7 6 5 4 3 2 1 0

1

5V GndPOWER

www.arduino.cc

ANALOG INVin 0 1 2 3 4 5

Arduino UNO

IoTBroker

QEST

state-of-artapproach to IoT apps

QEST-basedsolution to IoT apps

Bridge

Web App

Device

Web App

Device

IoTBroker

QEST

state-of-artapproach to IoT apps

QEST-basedsolution to IoT apps

Bridge

Web App

DeviceSC

LS

DA

AR

EF

GN

D

IOR

EF

RE

SE

T3V

3P

WM

PW

M

PW

M

L

TXRX ON

ICSP

PW

MP

WM

PW

M TX

RX

31

21

11

01

9 8DIGITAL

7 6 5 4 3 2 1 0

1

5V GndPOWER

www.arduino.cc

ANALOG INVin 0 1 2 3 4 5

Arduino UNO

Page 29: Building a multi protocol broker for the internet of things using nodejs

QEST• retains every message received

: MQTT to REST

curl -H "Accept: txt" http://qest.me/topics/temp

client = PubSubClient(server, 1883, callback);client.publish("temp", "30");

• every topic has its own URI: /topics/<NAME>

Page 30: Building a multi protocol broker for the internet of things using nodejs

QEST• transform every HTTP PUT received to a MQTT message

: REST to MQTT

void callback(char* topic, byte* payload, int length) { ...}PubSubClient(server, 1883, callback);client.subscribe("temp");

curl -X PUT -d '{ "payload": 42 }' \ -H "Content-Type: application/json" \ http://qest.me/topics/temp

• devices can listen directly to MQTT topics

Page 31: Building a multi protocol broker for the internet of things using nodejs

How to scale

Redis

HTTP/MQTT Clients

REST Server MQTT Server

QESTData Layer

...

Load Balancer

REST Server MQTT Server

QESTData Layer

Page 32: Building a multi protocol broker for the internet of things using nodejs

How much time took me to write the first version of QEST?

Page 33: Building a multi protocol broker for the internet of things using nodejs

A day.

Thanks

How much time took me to write the first version of QEST?

Page 34: Building a multi protocol broker for the internet of things using nodejs

• Express

• Socket.io

• node_redis

• MQTT.js

• Coffeescript

A day.

Thanks

Page 35: Building a multi protocol broker for the internet of things using nodejs

What happens if you write a broker in a day?

(Hint)

Page 36: Building a multi protocol broker for the internet of things using nodejs

Spaghetti code!

Page 37: Building a multi protocol broker for the internet of things using nodejs

How to untangle it?

Page 38: Building a multi protocol broker for the internet of things using nodejs

Refactoring!!!

http://www.flickr.com/photos/adewale_oshineye/2933030620/

Page 39: Building a multi protocol broker for the internet of things using nodejs

1.A single file can go really far!

Well, the first improvement were two files

Page 40: Building a multi protocol broker for the internet of things using nodejs

Coffee-scriptis awesome for prototyping!

Page 41: Building a multi protocol broker for the internet of things using nodejs

Coffee-scriptis a real pain to debug

Page 42: Building a multi protocol broker for the internet of things using nodejs

2.Extracting a data layer!

REST Server

Redis

MQTT Server

QEST

Data Layer

HTTP Clients MQTT Clients

Page 43: Building a multi protocol broker for the internet of things using nodejs

Discover my roots

Page 44: Building a multi protocol broker for the internet of things using nodejs

Ruby Language

Page 45: Building a multi protocol broker for the internet of things using nodejs

Ruby Language

Ruby on Rails

Page 46: Building a multi protocol broker for the internet of things using nodejs

Ruby on Rails

The ActiveRecord

pattern is AWESOME!

Page 47: Building a multi protocol broker for the internet of things using nodejs

ActiveRecord

1. Best pattern for storing data2. Its main responsibilities are

CRUD operations3. I used that pattern to build a

cross-process pub/sub system

Page 48: Building a multi protocol broker for the internet of things using nodejs

Hubot © 2012 GitHub Inc. All rights

Page 49: Building a multi protocol broker for the internet of things using nodejs

3.Extracting an inter process pub/sub library

Page 52: Building a multi protocol broker for the internet of things using nodejs

Asco

ltator

ivar  ascoltatori  =  require('ascoltatori');

//  you  can  use  RedisAscoltatore,  ZeromqAscoltatore,//  RabbitAscoltatore,  MQTTAscoltatorevar  ascoltatore  =  new  ascoltatori.MemoryAscoltatore();

ascoltatore.subscribe("hello/*",  function()  {    //  this  will  print  {  '0':  "hello/42",  '1':  "a  message"  }    console.log(arguments);      process.exit(0);});

ascoltatore.publish("hello/42",  "a  message",  function()  {    console.log("message  published");});

Page 57: Building a multi protocol broker for the internet of things using nodejs

Matteo Collina

Software Engineer @ Mavigex

Ph.D. Student @ University of Bologna

matteocollina.com

Page 58: Building a multi protocol broker for the internet of things using nodejs

Matteo Collina ([email protected])

Thank You!

@matteocollina

http://www.flickr.com/photos/axel-d/479627824/