Photo by Ahmed Zayan on Unsplash

What is the MQTT Protocol and how does it work?

Introduction

Message queueing Transport Protocol ( MQTT ) is an extremely lightweight publish/subscribe messaging transport protocol. It is an open protocol that runs on TCP/IP and who’s architecture incorporates a client publisher(s), a client subscriber(s) and a central Broker.

The Client Publisher sends data to the broker to a particular topic, while the Client Subscriber gets data from the broker, by subscribing to a particular topic.

Summary Diagrammatically Representation of how MQTT protocol works

Topics | Clients | Broker

A client is an entity connected to the MQTT Broker with the aim of receiving, sending messages or both. A client can be a subscriber, a publisher or both. If a client is a publisher, their sole purpose is that of sending messages and if a client is a subscriber theirs is to wait and get messages that have been published by the client publisher.

In the event we have multiple client publishers and multiple client subscribers how do we distinguish which messages are meant for a particular client subscriber? This is where topics come in.

A Topic is basically a UTF-8 string that the broker uses to filter messages for each connected client, it enables the broker to identify the messages received and then expose the data to the clients subscribed to it.

A topic can consist of one or more topic levels separated by a forward slash eg. topic/house/lighting. The elements topic, house and lighting are known as topic levels while the forward slash is a topic level separator. The forward slash enables us to create topic levels under the main topic, in our case that would be sensor. Using topics help to reduce noise making it easy for client publishers to get the data they only need.

topic/house/lighting

Clients subscribe to particular topics so as to be able to receive the messages sent through the MQTT Broker. In essence, it is like telling a mailman, anytime there is a letter addressed to “John”, please deliver it to my office. Clients can also simultaneously subscribe to multiple topics using wildcards. Wildcards can either be single-level or multilevel in the following format:

  • Single level wildcards have ‘+’ in them eg : topic / house / + / temperature
  • Multi level wildcard have ‘#’ within the topic eg. topic / house / #

A Last Will and Testament (LWT), this is a feature built within the MQTT protocol that is used to notify other clients subscribers about an ungracefully disconnected client publisher. To be able to use it, one will need to set the lastWillTopic, lastWillMessage, lastWillRetain (for disconnected packets) and Quality Of Service(0, 1, 2).

It serves as a great way to notify client subscribers of a client publisher connection loss and is often combined with retainer messages to store the state of a client on a specific topic . It is best to use this feature only when needed.

An MQTT Broker — this is where the muscle of the MQTT structure. The MQTT Broker handles all the operations involved in filtering messages received based on their topics and exposes them to the respective subscribers. The Broker servers are able to store the messages even after sending them. The broker does not know or need to know the identity of the client sending the payload but only checks for the topic linked to the message.

An Illusatration of How MQTT Broker works

The assurance of delivery of message exchange between the publisher and the subscribers is important especially for mission critical and real time applications. This introduces the topic of the Quality Of Service (QoS) levels.

Quality Of Service (QoS)

The Quality Of Service (QoS) level is an agreement between the sender and the receiver of a message that defines the guarantee of delivery for a specific message. There exist 3 QoS levels:

  • QoS 0: Client to Send Data At least once.
  • QoS 1: Client sends the message and awaits an acknowledgement (ACK) from the broker, if it does not receive it, it resends the message till it gets the acknowledgement (ACK) .
  • QoS 2: Client sends the message and awaits an acknowledgement (ACK) from the broker, after it has received the ACK it then sends back a RELEASE message to the server and expects another ACK, if there exists a duplicate message it does not send duplicate messages to the subscriber clients .

Advantages of this MQTT protocol

  • Extremely Lightweight
  • Easy to use or integrate
  • Due to its message structure it has a lower message size as compared to HTTP protocol.
  • Uses push based mechanism standing TCP connections
  • It supports bi-directional messaging.

Where is this protocol best used ?

Generally the protocol is best for machines to machine communications in constrained environments. Some use cases are Internet of Things Applications e.g. Smart Farming solutions and Real Time m-Messaging Applications.

Summary

MQTT Summary showing What, Why and How MQTT works.
What……. Why…….How ?

Note : In this piece i would like to acknowledge that this was a collaborative piece that was written by Sleeanne and I amid deployment of an IoT Solution.

Thank you very much..

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Joseph Ridge

Joseph Ridge

Artist (visual) | Rider| Junior Software Engineer. #Live_Love_Laugh