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.
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
Topics
Topics are defined as a hierarchical, case sensitive string using slashes as a delimiter and can be specific,
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”
- “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.
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.