Skip to content

MQTT essentials

Learn MQTT. Build reliable connected systems.

Explore MQTT from the fundamentals to production-ready architectures. Learn how to connect devices, applications, and industrial systems, move data reliably in real time, and scale from your first MQTT project to enterprise IoT and IIoT deployments.

HiveMQ wrote the book on MQTT

You're in the right place for expert guidance and resources whether you're new to MQTT, building out your IoT/IIoT platform, working on OT/IT integration or building the data backbone for your AI initiatives.
Get a comprehensive MQTT guide

What is MQTT?

The MQTT protocol is the de-facto standard for IoT messaging. Standardized by OASIS and ISO, MQTT publish/subscribe protocol provides a scalable and reliable way to connect devices over the Internet. Today, MQTT is used by many companies to connect millions of devices to the Internet.

HiveMQ helps write the MQTT standard. HiveMQ is a member of the OASIS MQTT Technical Committee, and HiveMQ's Simon Johnson serves as Co-Chair of the Technical Committee and Chair of the MQTT-SN Subcommittee, directly shaping standards like MQTT-SN 2.0.

Why companies use MQTT

  • It requires minimal resources since it is lightweight and efficient
  • Supports bi-directional messaging between device and cloud
  • Can scale to millions of connected devices
  • Supports reliable message delivery through 3 QoS levels
  • Works well over unreliable networks
  • Security enabled, so it works with TLS and common authentication protocols

Key features

MQTT client icon

MQTT clients

MQTT clients publish a message to an MQTT broker and other MQTT clients subscribe to messages they want to receive. Implementations of MQTT clients typically require a minimal footprint so are well suited for deployment on small constrained devices and are very efficient in their bandwidth requirements. HiveMQ developed a Java-based MQTT client that is available under an open source license.
MQTT broker icon

MQTT broker

MQTT brokers receive published messages and dispatch the message to the subscribing MQTT clients. An MQTT message contains a message topic that MQTT clients subscribe to and MQTT brokers use these subscription lists for determining the MQTT clients to receive the message. HiveMQ offers a commercial and open source MQTT broker.
MQTT quality of service icon

MQTT QoS (quality of service) levels

MQTT implements 3 quality of service levels for agreement between the sender and receiver: 1) At most once (0), 2) At least once (1), and 3) Exactly once (2). These QoS levels allow for more reliable IoT applications since the underlying messaging infrastructure and adapt to unreliable network conditions.
Persistent session icon

Persistent sessions

MQTT allows for a persistent session between the client and the broker. This allows for sessions to persist even if the network is disconnected. Once the network is reconnected, the information to reconnect the client to the broker still exists. This is one of the key features that makes the MQTT protocol more efficient than HTTP for use over unreliable cellular networks.
Retained message icon

Retained messages

MQTT clients that subscribe to a new topic have no insight into when to expect the first message they will receive. However, an MQTT broker can store a retained message that can be sent immediately upon a new MQTT subscription. In this case, the MQTT client will receive at least one message upon subscribing to the topic.
Last will and testament icon

Last will and testament (LWT)

An MQTT client can specify to an MQTT broker a message, called the last will, that will be sent if the MQTT client ungracefully disconnects. This allows for a more graceful system wide notification that a client has been disconnected.

MQTT basics: What is MQTT and how does it work?

MQTT basics

Start with the fundamentals of MQTT and build from there.

Learn how MQTT works, from publish/subscribe messaging, clients, brokers, and topics to the patterns that power connected IoT and IIoT systems. Whether you're new to MQTT, building an application, connecting industrial assets, or evaluating MQTT for a larger data architecture, these resources will help you understand the fundamentals and put them into practice.

More on MQTT

Learn more about MQTT and MQTT brokers.

MQTT packets: A comprehensive guide

Explore MQTT control packets, the core of IoT communication. Learn their structure and types to design and test MQTT-based systems.

MQTT broker comparison – Which is the best for your IoT application?

In-depth comparison of open-source Vs. commercial Vs. cloud MQTT brokers for IoT/IIoT devices and applications.

Creating highly available and ultra-scalable MQTT clusters

Learn how MQTT Broker Clusters are essential for high availability and scalability to solve the critical part of a messaging infrastructure.

MQTT topic tree & topic matching: Challenges and best practices explained

Learn how to find matching subscriptions among millions of MQTT subscribers.

MQTT over Websockets

Learn to send and receive MQTT messages directly in a browser.

A beginner's guide to MQTT brokers

Learn how to set up an MQTT broker, how to choose the right one for your IoT/IIoT use case & more with this guide.

Implementing MQTT in Java

Learn how to set up an MQTT client and a broker connection in Java.

Implementing MQTT in JavaScript

Learn how to set up an MQTT client and a broker connection in JavaScript.

Implementing MQTT in C

Learn how to set up an MQTT client and a broker connection in C.

Implementing MQTT in C Sharp

Learn how to set up an MQTT client and a broker connection in C Sharp.

Implementing MQTT in Python

Learn how to set up an MQTT client and a broker connection in Python.

15 frequently asked MQTT questions

Top 15 frequently asked questions or FAQs about MQTT protocol, such as does MQTT work with Apache Kafka, does MQTT require internet, etc.

HiveMQ vs. Mosquitto: An MQTT broker comparison

Compare HiveMQ’s scalability and clustering with Mosquitto’s lightweight design to choose the right MQTT broker for your application needs.

HiveMQ vs. AWS IoT Core: A comparative analysis for IoT messaging

Compare HiveMQ vs. AWS IoT Core on scalability, MQTT compliance, integrations, security, and vendor lock-in to choose the best IoT messaging platform.

MQTT glossary

Get to Know the Key MQTT Terminologies at a Glance

The standardized and improved version of the MQTT specification that the OASIS technical committee released in 2014.

The latest version of the MQTT specification. The OASIS MQTT 5 release features significant enhancements for scalability and large-scale systems.

An MQTT broker is a server that receives all messages from the MQTT clients and then routes the messages to the appropriate destination clients. The broker is at the heart of any publish/subscribe protocol.

The MQTT 5 feature that allows the MQTT client to define whether the MQTT broker continues a previous session for the client or starts a fresh session. The clean start flag is set in the CONNECT package of the client.

Any device (from a microcontroller up to a fully-fledged server) that runs an MQTT library and connects to an MQTT broker over a network. Both publishers and subscribers are MQTT clients.

The client identifier (ClientId) identifies each MQTT client that connects to an MQTT broker. The broker uses the client ID to identify the client and the current state of the client. This ID should be unique per client and broker.

The connection between an MQTT client and the MQTT broker. MQTT clients never connect directly. Once the MQTT connection is established, the broker maintains the connection until the client sends a disconnect command or the connection breaks.

Refers to the growing network of Internet-connected devices around the world that can send and receive data over a wireless network with little or no human assistance.

A subset of IoT that enhances manufacturing and industrial processes. IIoT brings together smart devices, machine learning, advanced analytics, computers, and people to create intelligent industrial operations.

Keep-Alive number of seconds that the broker permits between when a client finishes sending one MQTT packet and starts to send the next.

The last will message is part of the Last Will and Testament (LWT) feature of MQTT. The will message notifies other clients when a client disconnects ungracefully.

In MQTT, control packets represent the basic unit of data transfer. These packets are exchanged between servers and MQTT clients to perform various functions, including subscribing to topics and publishing messages.

Message Expiry is an MQTT 5 feature that allows the MQTT client to set the period of time that the MQTT broker stores a specific PUBLISH message for any matching subscribers that are not currently connected. For more details, read our Session and Message Expiry Intervals blog post.

A protocol that defines the rules, formats, and functions used to send messages between machines.

The MQTT protocol defines a series of predefined MQTT control packets (messages) to manage the communication between devices and the broker.MQTT Packet TypesEach MQTT packet contains a mandatory 2-byte header with an optional variable header and/or payload. The minimum MQTT packet size is 2 bytes. The maximum packet size is 256MB.AUTH: A packet that MQTT brokers and clients can send after connection establishment to realize advanced authentication methods such as challenge/response or OAuth. (MQTT 5 only) CONNECT: The packet the MQTT client sends to the MQTT broker to request a connection. CONNACK: The packet the MQTT broker sends to the MQTT client to acknowledge receipt of the connection request. DISCONNECT: The packet the MQTT broker or client sends upon disconnection. PINGREQ: The packet the MQTT client sends to the MQTT broker to establish the status of the server connection. PINGRESP: The packet the MQTT broker sends in response to a PINGREQ message from an MQTT client to indicate that the server is alive. PUBLISH: The packet the MQTT broker or client sends to publish a message to a topic. PUBACK: The packet the MQTT broker or client sends in response to a PUBLISH packet with QoS 1. PUBREC: The packet the MQTT broker or client sends in response to a PUBLISH packet with QoS 2. PUBREC is the second packet of the QoS 2 protocol exchange. PUBREL: The packet the MQTT broker or client sends in response to a PUBREC message. PUBREL is the third packet of the QoS 2 protocol exchange. PUBCOMP: The packet the MQTT broker or client sends in response to a PUBREL message. PUBCOMP is the fourth and final message of the QoS 2 protocol exchange. SUBSCRIBE: The packet that the MQTT client sends to the MQTT broker to subscribe to a topic. A SUBSCRIBE message can contain multiple subscriptions for a client. Each subscription is made up of a topic and a QoS level. SUBACK: The packet the MQTT broker sends to the MQTT client in response to a SUBSCRIBE packet. The message contains the packet identifier of the original subscribe message and a list of return codes. UNSUBSCRIBE: The packet the MQTT client sends to the MQTT broker to delete a subscription to a specific topic on the broker. UNSUBACK: The packet that the MQTT broker sends to the MQTT client to acknowledge the deletion of a subscription. The UNSUBACK message contains the packet identifier of the original UNSUBSCRIBE message.

The packet identifier uniquely identifies a message as it flows between the MQTT client and broker. The client library and/or the broker are responsible for setting this internal MQTT identifier.

The application-specific content of an MQTT packet. The payload contains the actual content of the message. MQTT is data-agnostic. It is possible to send images, text in any encoding, encrypted data, and virtually every data in binary.

The encryption of application-specific data on the application level (typically, the MQTT PUBLISH packet payload or the CONNECT LWT payload). Payload encryption allows end-to-end encryption of application data and adds another layer of security for topics that deliver confidential information.

A client session for which the MQTT broker saves all relevant client information. When the client reconnects, information such as subscriptions is immediately available. The session is identified by the client ID that the client provides during connection establishment. To know more, read our Persistent Session and Queuing Messages blog post.

An architectural pattern that decouples the clients that send messages (the publishers) from the clients that receive messages (the subscribers). In the pub/sub model, publishers and subscribers never contact each other directly. The connection is handled by a broker that filters all incoming messages and distributes them correctly to subscribers

The Quality of Service (QoS) level is an agreement between the sender of a message and the receiver of a message that defines the guarantee of delivery for a specific message. There are 3 QoS levels in MQTT.Quality of Service levelsQoS 0:This service level guarantees a best-effort delivery. There is no guarantee of delivery. The recipient does not acknowledge receipt of the message and the message is not stored and re-transmitted by the sender. QoS 1:This service level guarantees that a message is delivered at least one time to the receiver. The sender stores the message until it gets a PUBACK packet from the receiver that acknowledges receipt of the message. The message can be sent or delivered multiple times. QoS 2:This service level guarantees that each message is received only once by the intended recipients. QoS 2 is the safest and slowest service level. The guarantee requires at least two request/response flows (a four-part handshake) between the sender and the receiver.

Information the MQTT protocol supplies to indicate the result of the numerous operations. The SUBACK and UNSUBACK packets provide a list of reason codes per topic in the payload. Since MQTT v5, reason codes for CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, DISCONNECT, and AUTH packets appear in the variable header.

Optional human-readable content that can be provided with a specific MQTT reason code to further explain the outcome.

A normal MQTT message with the retained flag set to true. The MQTT broker stores the last retained message and the corresponding QoS for the topic. Each client that subscribes to a topic pattern that matches the topic of the retained message receives the retained message immediately after they subscribe. The broker stores only one retained message per topic.

The MQTT protocol is designed for very secure communication. As an application layer protocol, MQTT offers extensive device authentication and authorization possibilities. The underlying TCP/IP transport protocol of MQTT can add additional security via TLS encryption.

See MQTT broker.

Session Expiry is an MQTT 5 feature that allows the MQTT client to define how long after the client disconnects until the session of the client expires. When a session expires, the broker removes all subscriptions, queued messages, and incomplete message transmissions that are associated with the session.

A special type of subscription that allows multiple MQTT clients to share the same subscription. The clients in the shared subscription group receive messages in an alternating fashion. This MQTT 5 feature balances the message load of a single topic across all subscribers.

An open-source software specification for MQTT. Sparkplug is designed to help the manufacturing industry seamlessly integrate data from their applications, sensors, devices, and gateways.

Topic is an MQTT-specific form of addressing that allows MQTT clients to share information.

An MQTT 5 feature that allows MQTT clients to define an integer value that the client can use as a substitute for the name of a topic. Use of a topic alias in place of the UTF-8 string topic name can reduce the size of the PUBLISH package.

A label with UTF-8 encoding that the publisher of the MQTT message assigns to the MQTT message. The MQTT broker matches this label against the subscription requests that MQTT clients submit.

A text string with UTF-8 encoding that MQTT clients use to subscribe to topics and receive publications. The MQTT broker uses to filter to distribute matching messages to interested MQTT clients. An MQTT topic filter consists of one or more topic levels. Each topic level is separated by a forward slash (topic level separator). MQTT topic filters support the use of wildcards.Topic FiltersMQTT Wildcards:Predefined symbols that can be used in an MQTT topic filter to subscribe to multiple topics simultaneously. Plus sign (+):A single-level wildcard that matches any name for a specific topic level. Hash (#):A multi-level wildcard that matches all topics that match the topic levels that are specified in front of the # symbol in the topic filter.

MQTT topics that start with a $ symbol are reserved for internal statistics of the MQTT broker. Clients cannot publish messages to $ topics and $ topics are excluded from wildcard subscriptions.

Get the help you need.

Ask our experts or learn more about MQTT as you work to connect, communicate, and control your IoT data.