MQTT in a Nutshell
— Part One, What is it
What is MQTT? The most expressive answer to this question I’ve ever seen is this one on the mqtt.org website:
“MQTT is a machine-to-machine (M2M)/’Internet of Things’ connectivity protocol.”
It States Quite clear, the MQTT is a connectivity protocol for M2M and Internet of Things (IoT). Seems like to understand MQTT, we’d need to understand M2M or IoT first.
M2M and Internet of Things
Lots of people are already using Internet of Things, some of them are just not aware of it. Recently I’ve visited a friend, he was quite excited about his new smart oven and can’t wait to show me how smart the oven is: Recipe was downloaded to the smartphone and sent to the oven via WiFi; during the baking it was remotely adjustable via a smartphone app; when the baking was done, the oven sent notice to the smartphone. Well, he is already in the IoT era.
The basic concept of Internet of Things is not new, it is the inter-networking of devices, let the devices collect and exchange data by themselves. Sometimes this kind of data exchange happens even without human intervention (machine to machine connectivity). The devices are integrated with each other via network. Nowadays, the scale of this kind integration can be enormous, it can result in improved efficiency, accuracy and economic benefit and reduced human intervention.
In the world of IoT, almost everything (including human) can be connected. Connecting of things has the potential to bring new benefit, what we are going to do is using our curiosity and imagination to release this potential. Gartner says by 2020 there will be over 26 billion connected devices. Computer, cellphone, tablet, table lamp, coffee maker, car, light sensor, motion detector, biochip, plane and anything you can imagine. Some suggest regarding “things” as an “inextricable mixture of hardware, software, data and service”.
One key element of IoT is, that the “things” have the ability to connect. They need a common language that everybody understands, a connectivity protocol. There are already a lot of connectivity protocols in the world, over the years, some protocols like http are used by millions of applications. Why not just one of the widely used protocols? Why MQTT for IoT?
“Anything that can be connected, will be connected”. Think about this: at 5:30 a.m. your smartphone tells the heating regulator in the living room to adjust the room temperature comfortable for daily activity; at 5:50 a.m. your phone tells the coffee maker to start making coffee; when you are waked up by the phone at 6 a.m., everything is ready, even your favourite show is downloaded to you tablet. While you are enjoying your peaceful morning, you may notice one thing: all the connected devices are so different and they work together well.
We just talked about a scenario of IoT, we call it smart home. What about we build a smart city or even smart world? In this world, a super-computer may exchanges data with a system on chip. E.g. Healthcare centre monitoring an implanted chip of patient and send alert to the patient’s doctor. What kind of communication protocol should be used, when any device is allowed and the devices are geographically widely distributed?
IT must be a lightweight protocol with a small code footprint, and a very limited network bandwidth should be sufficient for this protocol. MQTT is this kind of protocol.
“MQTT is a Client Server publish/subscribe messaging transport protocol. It is light weight, open, simple, and designed so as to be easy to implement. These characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts where a small code footprint is required and/or network bandwidth is at a premium.”
— Cited from the MQTT specification
A little History about MQTT
MQTT stands for MQ Telemetry Transport. It was first invented by Andy Stanford-Clark and Arlen Nipper in 1999. They were on a pipeline SCADA system (where the word Telemetry came from) and needed real-time delivery of data on satellite link and dial-up line to connect the remote pipelines. With these constrained resource they start to talk about a new publish and subscribe protocol, that is MQTT.
MQTT was designed with the following five goals:
It needed to be simple to implement;
It needed to have a quality of service data delivery;
It needed to be lightweight and bandwidth efficient;
It had to be data agnostic;
And it had to have continuous session awareness.
Today these five goals are still stand, they are essentially identical with the needs for IoT.
In 2010 IBM release the MQTT 3.1 royalty free and OASIS standardized the protocol in 2014.
MQTT 3.1.1 is the current version as this article is being written. The next version of MQTT is 5.0, a working draft of MQTT 5.0 is made public.
MQTT v.s. XMPP
Comparing the MQTT with other protocols may make it much easier to understand why the MQTT is suitable for the IoT, here we compare it with XMPP, another messaging protocol from the later nineties.
In 1999 The XMPP was invented. It is an eXtensible Messaging and Presence Protocol.
- MQTT uses a one-byte fixed header and two-byte KeepAlive packet, its packet has a size and simple to en/decode. While XMPP is encapsulated in XML, it is large in size and complicated in interaction.
- MQTT uses topic for routing, it is more flexible than XMPP’s peer to peer routing based on JID.
- MQTT protocol doesn’t define a payload format, thus it carries different higher level protocol with ease. While the XMPP uses XML for payload, it must encapsulate binary in Base64 format.
- MQTT supports message acknowledgement and QoS mechanism, which is absent in XMPP, thus MQTT is more reliable.
to be continued…