MQTT – An introduction – Background



MQTT – An introduction – Background

0 2


mqtt-slides

Presentation about MQTT, a messaging protocol for M2M/IoT applications

On Github amorenoc / mqtt-slides

MQTT

An introduction

Alexandre Moreno

MQ Telemetry Transport

MQTT is a machine-to-machine (M2M)/Internet of Things connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. High-latency networks are addressed by the protocol. Consider the temporary losses from radio effects such as fading in wireless links. For example, it has been used in sensors communicating to a broker via satellite link. It is also ideal for mobile applications because of its small size, low power usage, minimised data packets, and efficient distribution of information to one or many receivers. http://mqtt.org

buzzwords

Internet of ThingsMachine to MachineSmart Grid

Useful background reading to understand the use-cases

Purpose of MQTT?

  • Fill the gap between the numerous devices and applications that can produce data and the wider world of data consumers
  • Good fit for simple push messaging scenarios

At a glance...

Scalability

“a single 1u-form-factor appliance can handle up to 1 million sensors and 13 million concurrent messages” link to article many ideas: mqtt can use websocket as a transport, so essentially you could have a web application performing sensor readouts.

Interoperability

MQTT Interop Testing Day The goal is to have as many different MQTT client and server implementations participate in interoperability testing to validate the implementation of the upcoming OASIS MQTT standard. https://wiki.eclipse.org/Paho/MQTT_Interop_Testing_Day

Background

Publish/Subscribe pattern

Publish/Subscribe Messaging Model

  • Clients subscribe to topics (SUBSCRIBE)
  • Messages are published to a specific topic name (PUBLISH)
  • A broker server handles the routing of messages
Analogy: anonymous bulletin boards

Publish/Subscribe Messaging Model

  • Pros
    • Greater network scalability and a more dynamic network topology
    • Decoupling of applications
  • Cons
    • Same security vulnerabilities as Client/Server model

Message broker

  • Authenticates clients
  • Validates, transforms and routes messages
  • Performs topic-based message routing
  • Caches messages for delivery at a later timee.g. Will messages, RETAIN flag
  • Bridges: brokers can be connected together

The protocol

Message types

                        var MESSAGE_TYPE = {
                            CONNECT: 1, 
                            CONNACK: 2, 
                            PUBLISH: 3,
                            PUBACK: 4,
                            PUBREC: 5, 
                            PUBREL: 6,
                            PUBCOMP: 7,
                            SUBSCRIBE: 8,
                            SUBACK: 9,
                            UNSUBSCRIBE: 10,
                            UNSUBACK: 11,
                            PINGREQ: 12,
                            PINGRESP: 13,
                            DISCONNECT: 14
                        };
					

port numbers

TCP/IP port 1883 for MQTT. Port 8883 for MQTT over SSL.

Message format

Fixed header (2 bytes) + Variable header + Payload

Fixed header format

bit 7 6 5 4 3 2 1 0 byte 1 Message Type DUP flag QoS level RETAIN byte 2 Remaining Length

Topics/Subscriptions

  • Hierarchical structure of topics
    • e.g. sensors/temperature
  • wildcard pattern matching for subscriptions
    • multi-level with ‘#’
      • e.g. sensors/# matches both sensors/foo and sensors/foo/bar
      • # matches all topics
    • single-level with ‘+’
      • e.g sensors/+ matches sensors/foo and sensors/bar, but not sensors/foo/bar
The hierarchical structure, gives you a topic tree, ressembling a file system

Flags

QoS

The Quality of Service used to deliver a message

  • 0: Best effort
    • PUBLISH
  • 1: At least once
    • PUBLISH + PUBACK
  • 2: Exactly once
    • PUBLISH + PUBREC + PUBREL + PUBCOMP

Quality of Service levels and flows

Implementations

Mosquitto

An umbrella project, providing an open source MQTT v3.1/v3.1.1 broker, client libraries, language bindings, and client utilities.

Seems the Mosquitto project is moving to Eclipse, discussed next

mosquitto.org There are at least a C and Python implmentation, and a C++ binding

Examples

Mosquitto

C client

#include <stdio.h>
#include <err.h>
#include <mosquitto.h>

void on_message(struct mosquitto *m, void *user_data, const struct mosquitto_message *msg)
{
    fprintf(stderr, "lights at %s are %s\n", msg->topic, (char*)msg->payload);
}

int main(int argc, char **argv)
{
    struct mosquitto *client;
    int ret;
    
    mosquitto_lib_init();

    client = mosquitto_new("client-id", true, NULL);
    if (!client)
        err(1, "mosquitto failed");

    ret = mosquitto_connect(client, "127.0.0.1", 1883, 60);
    if (ret != MOSQ_ERR_SUCCESS)
        err(1, "mosquitto failed");

    ret = mosquitto_subscribe(client, NULL, "switches/+/status", 0);
    if (ret != MOSQ_ERR_SUCCESS)
        err(1, "mosquitto failed");

    mosquitto_message_callback_set(client, on_message);

    while (MOSQ_ERR_SUCCESS 
        == mosquitto_loop(client, -1, 1));

    return 0;
}
                        

Mosquitto

Python client

#!/usr/bin/env python
import mosquitto

def on_message(mosq, obj, msg):
    print("lights at "+msg.topic+" are "+msg.payload)

client = mosquitto.Mosquitto()
client.connect("localhost")
client.subscribe("switches/+/status")
client.on_message = on_message

while client.loop() == mosquitto.MOSQ_ERR_SUCCESS:
    pass
                        
A simple example showing how to subscribe to a topic and define a function to receive the messages. No client id is provided, so the mosquitto module will generate a random id, and reduce the chance of having an id collision on the broker Note that we are blocking; We could also use loop_start, which will handle the mosquitto connection into a separate thread.

Eclipse Paho

Project providing open source implementations of C, C++, Java, JavaScript, Lua and Python client libraries, plus a client view plugin for Eclipse IDE.

http://www.eclipse.org/paho Python client is the same as Mosquitto, just different license

Eclipse Paho

JavaScript MQTT client

client = new Messaging.Client( "127.0.0.1", 80, 'clientId' );

client.onMessageArrived = function( msg ) {
  console.log( "lights at " + msg.destinationName +
               " are " + msg.payloadString );
  client.disconnect(); 
};

client.connect({
  onSuccess:function() {
    client.subscribe( "switches/+/status" );
  }
});
Browser-based library that uses WebSockets to connect to an MQTT server.

To use MQTT over WebSocket, you'll need that server supports the WebSocket protocol, e.g. lighttpd with mod_websocket

Any projects making use of MQTT?

The house that Twitters

“Monitoring things such as how much power our house is using can give us valuable insights into the cost of various appliances in the home,” he said. “Recently I was out and got a tweet saying water usage was higher than normal. I phoned home and my wife looked out of the window to see the garden hose had been left on. “This can help us take steps to reduce our carbon footprint and reduce energy bills. Mine has dropped by a third in the last year. Telegraph article

Facebook messenger

chat sessions, where users can join and leave a conversation, fit well with the publisher-subscriber model

Under the hood: Rebuilding Facebook for iOS

Smart Lab

Monitoring experiments at the University of Southampton’s chemistry lab.

Heart pacemakers

Send cardio data to doctors remotely monitoring at home patients

Other messaging protocols

AMQP

XMPP

請問有其他問題嗎?

謝謝