Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

MQTT protocol

MQTT (Message Queuing Telemetry Transport) is an application layer protocol typically run over TCP, commonly used as a backend for cloud platforms.

MQTT is a publish-subscribe network protocol that consists of a message broker and one or more clients. Information is organized via topics; clients can publish and subscribe to specific topics through the MQTT broker. When publishing to a topic, all clients subscribed to that topic will receive the message from the MQTT broker.

MQTT broker

An MQTT broker is essentially a server that receives messages from connected clients and routes them to the applicable destination clients. Multiple clients can be subscribed to a single topic, and multiple clients can publish to the same topic as well.

MQTT allows for the decoupling of the client-side and server-side, so connected clients are unaware of each other’s information.

MQTT network structure

The MQTT protocol has a keep-alive function that ensures the connection between the broker and the client stays alive. When connecting to a broker, the client specifies a keep-alive interval, the maximum time the broker and client can go without communicating before the connection is closed. The broker can also set a keep-alive time; if the broker’s keep-alive time is shorter, this can close the connection on the broker-side.

MQTT control packets

The MQTT client and broker exchange MQTT control packets when performing work such as establishing a connection and subscribing to topics.

We will cover the most essential ones below.

CONNACK

The MQTT client always initiates the connection to the broker, using the CONNECT packet. The CONNACK packet is sent as a response to acknowledge the connection request. This packet contains a field that indicates the result of the connection.

SUBACK

After a connection is establish, the MQTT client will typically subscribe to relevant topics by sending a SUBSCRIBE packet the broker. The broker will then send a SUBACK packet as a response to acknowledge the subscription request. This packet contains a field that indicates the result of the subscription.

PUBLISH

The PUBLISH packet is used to publish messages. When the client receives a PUBLISH message from the broker, it means a message was published to a topic that the client is subscribed to.

DISCONNECT

The client and broker can both send a DISCONNECT packet before closing the network connection.

Quality of service

When connecting to the broker, the client can specify a quality of service (QoS) level.

  • At most once (0) – The message is sent only once, with no acknowledgment receipt.
  • At least once (1) – The message is re-tried by the sender multiple times until acknowledgment is received.
  • Exactly once (2) – The sender and receiver engage in a two-level handshake to ensure the message is received only once.

Persistent session

When requesting a connection with a broker, the MQTT client can request to use a Persistent Session. This will store some information, so in the case of a network outage the client does not have to set up a brand new connection with the broker. When returning to a Persistent Session after a network outage, the topic subscriptions will be stored, as well as QoS 1 and 2 messages published to the broker during the offline period.

Security

MQTT supports numerous authentication and data security mechanisms. In this course, we will focus on connection to the MQTT broker via TLS. Adding TLS will encrypt the communication between the MQTT client and broker. We will take a look at how to implement TLS in an MQTT connection in Exercise 2.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.