What Is MQTT? // Technology


Watch "What Is MQTT? // Technology" on YouTube.

YouTube video: What Is MQTT? // Technology


In this QuickBits episode we’ll be seeing why MQTT was invented, what it is, how it works and why it’s so popular with FaceBook, Amazon and Makers alike.

ERROR:not .src - map[alt: class: control: ctx:0xc017dff0e0 height: href: id:iRUI3y3all4 inline: size: span: src: style: thumbnail: title: width: xml:]

Background

MQTT was created in 1999 by Andy Stanford-Clark and Arlen Nipper out of a need to send sensor data from oil pipes in the desert over satellite links.

Andy had to solve the problem of communicating with low power, low CPU, low memory sensors instantaneously over unreliable, low bandwidth wireless and satellite networks.

This came at a time when other protocols such as AMQP, STOMP, CAP, XMPP, Restful HTTP and WAMP had been around for a while.


What is MQTT?

MQTT stands for Message Queue Telemetry Transport and is a publish/subscribe communication framework which allows embedded devices, and applications, to communicate freely with each other across low bandwidth networks using a Broker.


How does it work?

Anything that connects to the Broker is considered a Client and can be a

Publisher; who sends messages to the Broker.
Subscriber; who receives messages from the Broker.
or both Publisher and Subscriber.
Subscribers never need to poll Publishers,
because Publishers will only send a message to the Broker on an event.
All Clients initiate and maintain a constant network connection with the Broker,
so if a Publisher vanishes from the network,
the Broker will notify Subscribers.
This also enables very responsive communications as there is no TCP/IP setup and tear-down for every message sent.

Topics

A Client can subscribe to one or more Publishers using Topics.

Topics are defined as a hierarchical, case sensitive string using slashes as a delimiter and can be specific,

for example in my house, in the bathroom,
there is a humidity sensor. - “MyHouse/Bathroom/humidity”

Or using wildcards, such as a plus sign which is a single level reference - “Farm/+/WaterLevel”

and could refer to all the water levels on a farm.

  • “Farm/House/WaterLevel”
  • “Farm/Shed/WaterLevel”
  • “Farm/Dam1/WaterLevel”
  • “Farm/Dam2/WaterLevel”

Or a hash at the end as a multi-level reference to all sensors on a particular vehicle - “Vehicles/Area42/Police_54/#”

  • “Vehicles/Area42/Police_54/Siren”
  • “Vehicles/Area42/Police_54/Speed”
  • “Vehicles/Area42/Police_54/Location”

Quality of Service

Publishers and Subscribers can specify one of three Quality of Service levels which helps the Broker decide the redelivery attempts of each message. Each QoS level has it’s advantage and disadvantage.

0 - At most once.

This is the lowest QoS level, where the message will be delivered once to the Subscriber with no ACK expected. The Broker will not store or attempt any re-delivery.

1 - At least once.

In QoS level 1, the message will be delivered at least once with an ACK from the Subscriber. It is possible for the Subscriber to see duplicate messages.

2 - Exactly once.

The highest QoS level guarantees the message will be delivered only once. This is also the slowest method as it relies on a strong handshake between Broker and Client.

If the Subscriber QoS level is higher than a Publisher QoS level the Broker will automatically downgrade the Subscriber QoS level.

Broker Responsibility

The Broker is responsible for several things:

  • Authentication and Authorization of Clients.
  • Receiving and storing messages sent from Publishers.
  • Forwarding messages to Subscribers.
  • Guaranteeing a Quality of Service for messages.

Brokers can also become a Client when acting as a proxy to other Brokers. However, this is a more advanced setup.


Advantages of MQTT.

So, what are the advantages of MQTT?

Simple protocol

First of all there’s no steep learning curve. The protocol is dead simple and MQTT Clients can be setup with very little effort with all the popular languages supported.

  • Easy to implement.
  • Support for C, C++, C#, Go, Java, JavaScript, .NET, Python, Perl, etc.
Small footprint

MQTT code runs well in a small footprint requiring minimal CPU and Clients can drop into ultra-low power mode to conserve battery further.

  • Low memory.
  • Low CPU.
  • Low power.
Lightweight protocol

The lightweight protocol reduces payload size, increases battery life and the number of sensors can scale up to thousands, or millions, with minimal infrastructure.

  • Small payload size
  • Increases battery life.
  • Scales up.

MQTT will also deliver messages without caring about the payload content and will decouple all Clients from each other avoiding any expensive application logic.

  • Data agnostic.
  • Device decoupling.
  • Easy logic.
Communications

Devices on the MQTT network have reliable, instantaneous, bi-directional, event driven communication over high latency, low bandwidth, unreliable networks. So no expensive polling of Clients.

  • Reliable.
  • Instantaneous.
  • Bi-directional.
  • Event driven.
  • Over High latency networks.
  • Over Low bandwidth networks.
  • Over Unreliable networks.

Disadvantages of MQTT.

However, there are downsides to MQTT.

Needs TCP/IP

You need a working TCP/IP stack. This can cause issues with Clients that don’t have Ethernet or Wireless. However, there is another protocol called MQTT-SN or MQTT-S which works over UDP and serial.

Open connections

Network connections from Client to Broker remain open indefinitely which places an extra burden on the Broker. However, many low end Brokers can be setup as a cluster enabling millions of Clients to connect.

Lacks encryption

MQTT lacks any encryption capabilities and is the responsibility of the application. Encryption does add extra overhead and it was a good thing to keep out of the protocol.


I hope this has given you a quick overview of what MQTT is and what it does.

Check out my other videos in this series where I go further into the MQTT protocol, show you some of the public MQTT Brokers you can use and implement a quick and dirty MQTT application.


Related

Mick Hellstrom avatar
About Mick Hellstrom
Hacker. Maker. YouTuber.

MickMake forums