What is MQTT?
MQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) telemetry in low bandwidth environments.
It was designed by Andy Stanford-Clark (IBM) and Arlen Nipper in 1999 for connecting Oil Pipeline telemetry systems over satellite.
Although it started live as a proprietary protocol it was released Royalty free in 2010 and became an OASIS standard in 2014.
There are two versions of MQTT.
The original MQTT which was designed in 1999 and has been in use for many years and designed for TCP/IP networks and the newer MQTT-SN which was specified in around 2013, and designed to work over UDP,ZigBee and other transports. See MQTT-SN working Notes.
MQTT-SN doesn’t currently appear to be very popular.
Here is the actual Specification MQTT V3.1
MQTT uses a publish /subscribe model which requires the use of a central Broker.
The publisher and subscriber never connect directly to each other and are unaware of each other.
It is like a radio or TV broadcast. The broadcast goes out even if there are no listeners.
How it Works
An MQTT client e..g. a sensor publishes a message to the broker.
The Broker sends the message to any MQTT clients that have subscribed to receive the data.
There is client software available in almost all programming languages and for the main operating systems Linux, Windows, Mac from the Eclipse Paho project.
On this site I will be using the Python client.
MQTT clients don’t have addresses like email addresses, phone numbers etc.
This means that you don’t need to assign addresses to clients like you do with most messaging systems.
Instead MQTT uses Topics.
As long as a topic name follows the naming standards it is accepted by the Broker.
A Topic name is a UTF-8 string, and must comprise of at least one character.
Topics can be created in a hierarchical manner (levels) using a forward slash as a delimiter.
Simple topic examples are:
topic = bulb1, topic = bulb2, topic = bulb3 or
topic =bulbs/ bulb1, topic =bulbs/ bulb2, topic =bulbs/ bulb3
MQTT Brokers or Servers
Note: The original term was broker but it has now been standardized as Server. You will see Both terms used.
There are many MQTT brokers available that you can use for testing and for real applications.
If you don’t want to install and manage your own broker you can use a cloud based broker from Cloud service providers like IBM, Microsoft (Azure) etc
Eclipse has a free public MQTT broker and COAP server that you can also use for testing. The address is iot.eclipse.org and the port is 1883 or 8883(SSL).
This article has a list of free cloud based brokers that can be used for testing.
This article has a list of self hosted brokers.
MQTT supports various authentications and data security mechanisms.
It is important to note that these security mechanisms are configured on the MQTT broker, and it is up to the client to comply with the mechanisms in place.
Client to Broker Communication
MQTT uses a command and command acknowledgement format.
That means each command has an associated acknowledgment.
You can always tell if a command has been successful by examining the acknowledgement message.
This video gives a useful introduction to MQTT
If you are familiar with the web and email then you will probably find, as I did, that MQTT is very different. These are some of the questions I had, and saw on other sites and forums that may clear things up a little.
Q- What happens to messages that get published to topics that no one subscribes to?
A- They are discarded by the broker.
Q-How can I find out what topics have been published?
A- You can’t do this easily as the broker doesn’t seem to keep a list of published topics as they aren’t permanent.
Q- Can I subscribe to a topic that no one is publishing to?
Q- Are messages stored on the broker?
A- Yes but only temporarily. Once they have been sent to all subscribers they are then discarded. But see next question.
Q- What are retained messages?
A- When you publish a message you can have the broker store the last published message. This message will be the first message that new subscribers see when they subscribe to that topic. MQTT only retains 1 message.
Real World Example Deployments
It’s often useful and interesting to see how a particular technology is actually being used. Here are some examples I’ve come across:
MQTT Version 5
The next version of MQTT (autumn 2017) will be version 5. If you are wondering what happened to 4 then see here.
MQTT By Example
Fortunately when I started to learn about MQTT I came across the very good MQTT essentials series that explained the main features of MQTT.
However there is one thing reading about something and another seeing it really work so I started using Python to create simple scripts to demonstrate some of the aspects of MQTT that I found confusing.
Before you start I would recommend that you read through the MQTT essentials articles as they cover the theory in more detail.
To create these example I used my own Mosquitto broker installed on my local network. However most of the examples would work using the public Mosquitto broker at iot.eclipse.org.
I use the Paho Python MQTT client for the client scripts.
However A knowledge of python isn’t really required to understand the examples.
MQTT by Example:
- Clean Sessions and QOS (quality of service) -Example
- Retained messages-Example
- Last will and testament-Example
- Username and Password authentication
Using the Python MQTT client
- Using the Paho MQTT Python Client for Beginners
- Client Objects-Working with The Python MQTT Client
- Client Connections-Working with The Python MQTT Client
- Subscribing to Topics Messages Using The Paho Python Client
- Publishing Messages Using The Paho Python Client
- Really good introductory series- Recommended reading MQTT essentials.
- Very good MQTT pdf with detailed examples.
- Installing and setting up The Mosquitto broker.