Professional Documents
Culture Documents
MQTT Study Workbook
MQTT Study Workbook
MQTT Study Workbook
Website: www.steves-internet-guide.com
Every precaution has been taken to ensure that the information presented in this book is accurate. However,
neither the author nor the publisher shall have any liability to any person or entity with respect to any loss or
damage caused or alleged to be caused directly or indirectly by the information contained within this work.
It will also be of use for those who want to get a quick overview of MQTT and MQTT features
without going into detail.
The only pre-requisites are a basic understanding of networking and networking protocols.
Introduction
MQTT stands for MQ Telemetry Transport but previously was known as Message Queuing
Telemetry Transport.
MQTT is a binary messaging protocol and transfers messages using a publish and subscribe
model.
A TV broadcaster broadcasts a TV program using a specific channel and a viewer tunes into this
channel to view the broadcast.
In MQTT a publisher publishes messages on a topic and a subscriber must subscribe to that topic
to view the message.
MQTT requires the use of a central Broker as shown in the diagram below:
MQTT Versions
There are two different variants of MQTT and several versions.
MQTT v3.1.0 –
MQTT v3.1.1 – In Common Use
MQTT v5 – Currently Limited use
MQTT-SN – See notes later
The original MQTT was designed in 1999 and has been in use for many years.
There is very little difference between v3.10 and 3.1.1, MQTT v3.1.1 is the version in common
use. MQTTv5 is now available (2022) on all of the main brokers and many of the clients.
MQTTv5 offers all of the functionality of MQTT v3 but with many new features.
MQTT Topics
MQTT topics are a form of addressing that allows MQTT clients to share information.
MQTT Topics are structured in a hierarchy similar to folders and files in a file system using the
forward slash ( / ) as a delimiter.
Using this system you can create a user friendly and self-descriptive naming structures of your
own choosing.
Except for the $SYS topic there is no default or standard topic structure.
That is there are no topics created on a broker by default, except for the $SYS topic.
All topics are created by a subscribing or publishing client, and they are not permanent.
A topic only exists if a client has subscribed to it, or a broker has a retained or last will
messages stored for that topic.
MQTT QOS
MQTT supports three QOS levels which are designed to ensure message delivery.
The QOS levels are a way of guaranteeing message delivery and they refer to the connection
between a broker and a client.
QOS 0 – Once
This is the fastest method and requires only 1 message. It is also the most unreliable transfer
mode.
Once the message has been sent by the client it is deleted from the outbound message queue.
If it receives an acknowledgement then it notifies the client app, and deletes the message from
the outbound queue.
If it doesn’t receive an acknowledgement it will resend the message with the DUP flag set
(Duplicate Flag).
The message will continue to be resent at regular intervals, until the sender receives an
acknowledgement.
If the message is being sent to the broker then the broker will forward that message to
subscribers even though the duplicate flag is set.
Questions
1. The latest version of MQTT is version 4?
2. How do you assign an address to a client?
3. What are topics?
4. Is the topic house/test the same as house/TEST?
5. Do you need to start the topic structure with a forward slash (/)?
6. Is house/te* a valid wild card usage?
7. Before you can publish a message on a topic you first need to create that topic on the
broker? True or False?
Answers
1. No it is version 5. There was no version 4
2. You don’t assign addresses to clients.
3. Topics are a form of addressing that clients publish and subscribe to.
4. No topics are case sensitive
5. No and it is generally not done
6. No wild cards can only be used directly after of before the / separator.
7. False.
Clean Sessions
When a client connects to a broker it can connect using either a:
Non-persistent connection (clean session) or a;
Persistent connection
With a non-persistent connection the broker doesn’t store any subscription information or
undelivered messages for the client.
In this mode the broker/server will, depending on the QOS of the published messages and
subscribing client, store subscriptions and messages for the client if it is disconnected.
Retained Messages
Normally if a publisher publishes a message to a topic, and no one is subscribed to that topic the
message is simply discarded by the broker.
However the publisher can tell the broker to keep the last message on that topic by setting the
retained message flag.
This can be very useful, as for example, if you have sensor publishing its status only when
changed e.g. Door sensor. What happens if a new subscriber subscribes to this status?
Without retained messages the subscriber would have to wait for the status to change before it
received a message.
However with the retained message the subscriber would see the current state of the sensor.
What is important to understand is that only one message is retained per topic.
The next message published on that topic replaces the last retained message for that topic.
Keep Alives
MQTT uses a TCP/IP connection.
This connection is normally left open by the client so that it can send and receive data at any
time.
If no data flows over an open connection for a certain time period then the client will generate a
PINGREQ and expect to receive a PINGRESP from the broker.
This message exchange confirms that the connection is open and working.
The last will message is sent as part of the connection payload and is optional.
Authentication
MQTT provides support for an optional username and password as part of the connection
message.
The username/password combination is transmitted in clear text and is not secure without some
form of transport encryption.
Most clients currently default to using MQTT v3.1. To use MQTTv5 you must set it in the
connection packet.
I’ve divided the features into sections to make it easier to determine where they apply.
Connection Features
Clean session/start
Client Restrictions/Limitations
Server Restrictions/Limitations
Will Delay Interval
User properties
Server Redirect
Clean session/start
This has been renamed to clean start and works basically the same as before except that we now
have a session expiry timer.
However in MQTTv5 if you set the clean start to false you need to provide a session expiry
value. If you don’t then it is set to 0 which makes it basically the same as clean sessions True.
This means that if a client disconnects and reconnects within 5 minutes with clean start=
False,QOS>1 then session state data (e.g subscribed topics, queued messages) are retained.
However, if the client disconnects and reconnects after 5 minutes with clean start= False,QOS>1
then session state data (e.g subscribed topics, queued messages) are lost, and the client must re-
subscribe to topics, and messages sent while the client was disconnected are lost.
Client Limitations/Restrictions
A client can now indicate to a server various client restrictions. It can tell the server to:
Restrict size of messages to send to the client
Restrict the number of QOS 1 and 2 messages
Uses: Used by low powered clients that lack the memory to store the messages. See MQTT and
Mosquitto Message size restrictions
Receive Maximum
The client tells the server to limit the number of QOS 1 and 2 messages that can be sent
concurrently.
Uses: Again used by low powered clients as QOS 1 and 2 messages mean that the client needs to
send additional acknowledgement messages.
Server Limitations/Restrictions
Providing certain functionality can strain server resources and so the server may not support
them.
It is now possible for the server to indicate this is the message exchange using the properties
field.
Example: The screen shot below shows the CONNACK message properties field From the
Mosquitto Server.
There are many other limitations that the server can indicate.
By default if the server doesn’t indicate that it doesn’t support a particular feature then it is
supported.
For example it is possible for the server not to support retain messages using the Retain
Available property, but in the screen shot above it is not there, and so the server supports retain
messages.
This prevents the will message being sent for a short connection interruption.
They can be used to send connection related properties from the Client to the Server.
The Server uses a Server Reference in either a CONNACK or DISCONNECT packet with Reason
code of 0x9C (Use another server) or Reason Code 0x9D (Server moved) as described in section
4.13.
Publish Features
Message Expiry
Payload Format Indicator
Topic aliases
User Properties
Request Response
Message Expiry
When a message is published with a QOS of 1 or 2 and the receiving client has:
1. Connected with clean session of False
2. Subscribed with a QOS or 1 or 2
Then the broker will hold published messages for the client if it is currently disconnected.
On MQTTv3 this was indefinite, but on v5 you can set the period that the server will hold the
message before discarding.
This is done in the publish message below is a sample Python code snippet:
client_pub.publish('test', msg_out1,qos=1,message_expiry_interval=20)
With the above setting then if the client doesn’t reconnect within 20 seconds the message is
discarded.
Uses: Useful at the receiver as is knows if it needs to decode the message payload.
User Properties
These are defined by the user and are a collection of key value pairs.
In Javascript =object
Python = dictionary
Example:
{msg_number:1,time:100}
keys=msg_number and time
These are useful for sending information to the destination outside of the payload.
In v3.1.1 this information was usually sent as JSON encoded data as part of the payload.
{msg_number:1,time:100,payload:payload_data}
This isn’t a problem if the payload is text data but if the payload is binary data then it will need
to be converted using Base64 encoding.
Topic Aliases
Topic aliases were introduced in MQTT-SN and are mechanism for reducing the size of
published packets by reducing the size of the topic field.
So if the topic houses/house1/upstairs/light1 had an alias of 1 then this could be used in place
of the topic string when publishing messages.
Before you can use topic aliases the client must indicate the topic alias maximum number to
the server.
By default if this isn’t set then it is 0 which means topic aliases aren’t allowed by the client.
MQTT uses a publish and subscribe pattern where there is no direct communication between
the sending client and the destination client/server.
Using the response topic in the publish message allows you to implement the request/response
pattern that is common in Web applications.
Subscribe
Non local publishing
Retained Message Control
Subscription Identifier
Shared Subscriptions
By Using the non-local option the broker will not send you any messages that you have
published.
Python Example:
client.subscribe('org/common',no_local=True)
Subscription Identifier
This is optional and if used it is an integer associated with the subscription and used to filter
incoming messages to the correct message handler.
However this could also be done by analysing the topic itself as in MQTTv3.1.1
Python Example:
client.subscribe('test', qos=0, subscription_identifier=1)
client.subscribe('test/#', qos=0, subscription_identifier=2)
Shared Subscriptions
The idea is to provide for client load balancing.
General
Reason Codes on All ACK Messages
Server Disconnect
All Acknowledgement packets (except PINGRESP) carry a reason code and there are 128 codes
available.
Uses and example : Makes it much easier for an application to understand what has gone wrong
and take the appropriate action.
A publish ACK with reason code 16 means that there are no matching subscribers. Therefore
in MQTTv5 it is possible to detect if a topic is in use to some degree.
Server Disconnect
In MQQTv3.1.1 only the client could send the disconnect message.
If the server encountered problems it would simply terminate the TCP/IP session.
In MQTTv5 the server can send the client a disconnect message along with a reason code.
Questions
1. MQTTv5 now contains a new Flag called Will retain? True or False
3. MQTTv5 now supports authentication methods other than username and password? True or
False
4. The authentication method property of the connect message is used to request extended
authentication? True or False
5 User properties let you insert your own Key/Value Pairs ? True or False
6 The request response is used identify the sending client name? True or False
7 In MQTTv5 the broker can inform you when you send too large a message? True or False
8. In MQTTv5 you always need to use a username and password? True or False
10. A client can inform a broker about the maximum packet size it will accept?
11. A client can choose not to receive retained messages? True or False
12 Shared subscriptions use a reserved topic root called $subscription? True or False
15. Once a topic alias is created by the client the client can subscribe using the topic alias? True
or False.
16. The number of topic Aliases allowed can be restricted? True or False
20. The AUTH control type is a new type added in MQTTv5 ? True or False
Answers
1.True 2.True 3.True 4.True 5.True
6.False 7.True 8.False 9.True 10.True
11.True 12.False 13.b 14:a 15.False
16.True 17.True 18.True 19.False 20.True