It was designed as an extremely lightweight publish/subscribe messaging transport. It is mostly useful for establishing connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. The protocol is data-centric, unlike for example HTTP, which is document centric.
The message structure is compact and the overhead of the binary header is just 2 bytes.
Every client has to be connected to the Message Broker to interact with the system, which sometimes makes the usage of the protocol not very efficient in cases of one message duration connections. Connecting and disconnecting to the broker has a resource cost which, in certain cases, can make the usage of the protocol as efficient as other protocol alternatives. Connection utilization is important in MQTT.
Shared SubscriptionsSession ExpirationMessage Expiration
In message queues, messages are stored in a queue until a consumer gets the message. In MQTT, messages that arrive in a topic without any subscribers are not kept in queues (unless a persistent session is enabled where all QOS1 & QOS2 messages, that are not yet confirmed or missed, are stored until consumption).
Messages in queues can be consumed by one client. In MQTT a message can have multiple subscribers. Message Queuing and Pub/Sub Messaging are two completely different messaging patterns.
The biggest difference with queues is that they need to be explicitly created and they also must be named. This is not the case with MQTT, where topics are flexible and created on the fly under a tree-like structure.
MQTT can provide a type of message queues under certain circumstances. This can happen when a persistent session is present between a client and the broker.
If a client that has a persistent session is disconnected from the broker and at some point reconnects, the following information is available immediately:
All the subscriptions of a clientAll QOS1 & QOS2 flow messages that have not been confirmedAll new QOS1 & QOS2 messages that the client missedAll QOS2 messages received from the client and are not yet completely acknowledged
By design, the MQTT protocol focuses on simplicity and mainly exposes 5 ways of interaction.
The message will be published or received at most once (fire and forget).
The message will be published or received at least once,meaning that the sender stored the message and will probably repeat the procedure if he does not receive an acknowledgment.
The message will be published or received exactly one timeand will include an extra verification step for the communicated message.
What is really worth making crystal clear, is that the involved entities in each communication scenario are three, the publisher, the broker, and the subscriber.
Publisher and subscriber are not designed to use the same type of service(QoS). Each client communicates only with the intermediate, the broker. Clients are not destined to know the existence of one another.
Let’s analyze an example.
Say we have a smart thermometer that connects to the message broker and publishes its temperature in a topic every 10 seconds in a “fire and forget” type of service. An app for example on the other side has subscribed to the temperature topic, in an “at least once” type of service, and displays the received temperature on the screen.
In this example, the smart thermometer has a very small network bandwidth footprint since it is publishing in a fire and forget manner. In most cases, temperatures do not tend to drastically change in the time frame of 10 seconds so QOS0 seems to be the most appropriate type to use.
The app, on the other hand, should display any differences in temperature, so it should receive any messages at least once.
This summarizes our design and brings us to the conclusion that involved entities, always depending on the case, should not use the same type of service.
Each client that publishes a message on the message broker, by design, has no awareness that anyone other than the broker has received the message. Messages could be identified as interests or topics of discussion that anyone interested, could receive knowledge of.
What I find particularly funny is that when a client subscribes to a topic, it has no knowledge, whether or not, the particular topic actually exists or has (LWT) or will have any data in it.
The only way to find out about them is to subscribe to a topic.
Topics that exist or will exist can have multiple or no subscribers at all.
MQTT topic structure is often mentioned as the topic tree. It is a tree-like structure that is split into levels. Topics defined by clients are UTF-8 strings that are processed by the broker to filter messages. Each topic consists of one or more topic levels. Topic levels are separated by forward-slashes “/”.
When subscribing to a topic, there are specific characters that can be used as wildcards. MQTT supports two types of wildcards. The single-level wildcard “+” and the multi-level wildcard “#”.
Let’s take the following topic tree as an example.
If we wanted to subscribe to messages regarding the humidity of the kitchen, all we would have to do is subscribe to:
What if we wanted to subscribe to the temperature of all the available rooms? We would use the single level wildcard character.
Last but not least, where would we subscribe to, if we wanted to listen to all messages regarding the kitchen and everything that is in there? This time we would use the multi-level wildcard character.
There are some best practices regarding topic structure that we will discuss later on.
One of MQTT’s main strengths is the small overhead it adds to communications. Its packet headers are compact and have a binary structure.
They consist of 3 parts at most.
Fixed header, present in all MQTT Control PacketsVariable header, present in some MQTT Control PacketsPayload, present in some MQTT Control Packets
The Fixed header is always present in each MQTT packet. It is two bytes in size. The detailed structure of the fixed header is given below.
The Variable header is not present in all MQTT control packets. Its structure is different for different MQTT requests.
Payload is the actual data which is going to be sent. It’s not present in all the MQTT control packets. The payload of the message is limited to 268,435,456 bytes.
The length of the actual topic string is at most 65536 bytes. This is a limit imposed by the MQTT spec. It is also worth noting that the topic is encoded in UTF-8, so there might be less than 65536 characters available.
The broker is always aware of all the connections that clients establish or terminate. There are also certain features that utilize this event.
By design, MQTT offers a very simple authentication mechanism.
The CONNECT Packet contains Username and Password fields.
An application might independently encrypt the contents of its Application Messages. This could provide privacy of the Application Message both over the network and at rest. This would not provide privacy for other properties of the Application Message such as Topic Name.
Client and Server implementations can provide encrypted storage for data at rest such as Application Messages that are stored as part of a Session.
The use of VPNs to connect Clients and Servers can provide privacy of data across the section of the network covered by a VPN.
Every broker that exists in the market has its own modules to handle authentication but generally they can be extended further to support authentication mechanisms such as those mentioned above. These implementations are usually custom, although some brokers on the market have some modules already implemented.
Although there are no restrictions on how Apps using the protocol are implemented, there are some practices that are widely adopted.
Topics are meant to categorize information into groups, in terms of interest. The topic structure should resemble a tree structure and should make possible the utilization of wildcards for subscriptions. Information regarding a specific topic should provide the ability to be retrieved by multiple entities.
To achieve that, consistency between topic layers should be maintained.Do not use a leading forward slash since it introduces another unused topic layer in the tree with a zero character that is not used. This can cause unnecessary confusion.
Never use spaces in a topic, UTF-8 has many different white space types and can cause trouble in debugging. Characters like these should be always avoided.
Never subscribe to “#”, the client might not be able to process the load and might also put some unnecessary strain on the broker. If you need, for some reason let’s say store all messages on a database, you should implement some kind of plugin on the broker to achieve this.
Specific topics and Extensibility should always be on the designer's mind since your topic structure should be able to extend. Topic name conventions should be maintained across all topics in order for your system to be able to adapt to new topic addition in the future without breaking the tree structure.
Use a unique identifier on topics that refer to specific entities. You should be able to distinguish each entity’s information or group it by a single level wildcard.
As we already mentioned, service levels can be different from client to broker & broker to client. Always take note that QOS2 service level is expensive and should be used only when it is absolutely necessary. QOS0 should be used only on non-sensitive data as message loss can occur. QOS1 should be used when receiving duplicate data will not cause any trouble.
In MQTT, establishing a connection is not a cheap task. Always avoid connections that will be utilized for a single information transaction. Maintain connection or send data to batches if possible. Single transaction connections almost ruin all the pros of the fast and compound nature of the protocol.
Security should never be taken lightly and TLS over TCP should always be used along with all the best practices that come along. Your connected entities will most likely be on a remote location meaning that your broker should be visible to the world. If your entities can execute commands or take orders from messages, then it’s not only a matter of security but also a matter of safety since sophisticated attackers can perform man-in-the-middle attacks at least.
There are several brokers out there that implement the MQTT protocol. Here are some of them that are my personal favorites.
VerneMQ — Is particularly interesting due to it’s scaling capabilities and provided MQTT features.
KubeEdge — (Mosquitto on Kubernetes) — having a broker “natively” on kubernetes.
Mosca-Mosquitto — I actually started out experimenting some years ago using Mosca. Mosca and Mosquitto are most likely the best brokers to learn, test and debug.
MQTT is a PUB/SUB communication protocol that enables light, fast and effective communication between entities. It has many features such as the multiple service levels, persistent sessions, last will & testament and many more. It implements a unique tree topic structure that enables flexible management of information. Last but not least, there are several brokers and clients out there can cope with the scaling demands of the present.