A Brief, but Practical Introduction to the MQTT Protocol and its Application to IoT
MQTT is a standardised publish/subscribe messaging protocol. It was designed in 1999 for use on satellites and as such is very light-weight with low bandwidth requirements making it ideal for M2M or IoT applications. As such, it has become one of the most common protocols for those situations.
What follows is a brief introduction to the protocol and some examples of its use. It is not intended to be a comprehensive reference on MQTT, but it should give enough information to get developers up and running. If you’re looking for a more complete discussion of the protocol, HiveMQ have published a series of articles available here.
Publish / Subscribe
The publish / subscribe (often called pub-sub) pattern lies at the heart of MQTT. It’s based around a message broker, with other nodes arranged around the broker in a star topology. This is a very different model to the standard client/server approach, and at first it might seem a little strange, but the decoupling it provides is a huge advantage in many situations.
Clients can publish or subscribe to particular topics which are somewhat like message subjects. They are used by the broker to decide who will receive a message. Topics in MQTT have a particular syntax. They are arranged in a hierarchy using the slash character (/) as a separator, much like the path in a URL. So a temperature sensor in your kitchen might publish to a topic like ‘sensors/temperature/home/kitchen’.
Let’s look at an example: Imagine a weather service which has a network of internet connected temperature sensors all over the world. All of these sensors maintain a connection to a broker and every ten minutes, they report the current temperature. They publish to a particular topic based on their location in the following format:
So a sensor on Baker Street in London would publish to ‘sensors/temperature/uk/london/baker_street’ with a message containing the current temperature.