MQTT and Node.js

Messaging in the Internet of Things

@matteocollina

MQTT

MQTT

Why Node.js matters?

MQTT.js

Instant Gratification

var client = mqtt.createClient();

client.subscribe("mqtt/demo");

client.on("message", function(topic, payload) {
  alert([topic, payload].join(": "));
  client.end();
});

client.publish("mqtt/demo", "hello world!");

Pattern Matching

var client = mqtt.createClient();

client.subscribe("mqtt/+");

client.on("message", function(topic, payload) {
  alert([topic, payload].join(": "));
  client.end();
});

client.publish("mqtt/demo", "hello world!");

Mosca

MQTT broker in Node.js

How can it work on a Browser?

Benchmark

Offline Mode

First, subscribe with QoS 1 and disconnect

var client = mqtt.createClient({
  clientId: "moscaslides",
  clean: false
});

client.subscribe("mqtt/offline", { qos: 1 },
  function() {
  
  alert("subscribe done!");
  // called when the subscribe is successful
  client.end();
});

Offline Mode

Then, someone else publish an important message

var client = mqtt.createClient();

client.publish("mqtt/offline", "hello world!", {qos:1},
  function() {
  
  alert("publish done!");
  client.end();
});

Offline Mode

Reconnect to receive the missed messages

var client = mqtt.createClient({
  clientId: "moscaslides",
  clean: false
});

client.on("message", function(topic, payload) {
  alert([topic, payload.toString()].join(": "));
  
  setTimeout(client.end.bind(client), 1000);
});

Does Offline Mode Scale?

Do you need a REST API?

Ponte API

http://github.com/mcollina

Thanks!


If you need help with node.js:


hello@matteocollina.com

@matteocollina on Twitter

www.matteocollina.com