learn/mqtt

What is an MQTT broker? How it works and what it does

Short answer

An MQTT broker is the server in an MQTT system. Clients never talk to each other directly: they publish messages to topics on the broker, and the broker forwards each message to every client that has subscribed to a matching topic. The broker also handles authentication, access control, sessions, QoS acknowledgements, retained messages and Last Will messages.

Updated:

What an MQTT broker does

An MQTT broker receives every message that clients publish and delivers it to every client that has a matching subscription. MQTT uses the publish/subscribe pattern. A sensor, PLC gateway or vehicle publishes a message to a topic such as plant/line-3/temp. A dashboard, historian or analytics service subscribes to that topic. The broker sits in between and does the routing.

This has three effects:

  • Publishers and subscribers are decoupled. A publisher does not need the address of any receiver. New consumers can be added without changing the device.
  • One message reaches many receivers. If ten applications subscribe to the same topic, the broker sends ten copies. The device sends one.
  • Clients only need one outbound connection. Devices connect to the broker over TCP. They do not need to accept incoming connections, which simplifies firewall rules at the Edge.

MQTT is defined by two OASIS standards that are both in wide use today: MQTT 3.1.1 (2014, also published as ISO/IEC 20922) and MQTT 5.0 (2019).

How a message flows through the broker

A message passes through the broker in four steps.

  1. Connect. The client opens a TCP (or TLS, or WebSocket) connection and sends a CONNECT packet with its client ID, optional credentials, keep alive interval and session settings. The broker answers with CONNACK.
  2. Subscribe. A consumer sends SUBSCRIBE with one or more topic filters, for example plant/+/temp. The broker answers with SUBACK and the QoS it granted for each filter.
  3. Publish. A producer sends PUBLISH with a topic name, a payload and a QoS level.
  4. Route. The broker matches the topic name against all subscriptions and sends a PUBLISH to each matching client.

You can see this with the Eclipse Mosquitto command line clients. In one terminal, subscribe:

mosquitto_sub -h broker.example.com -t 'plant/+/temp' -v

In a second terminal, publish:

mosquitto_pub -h broker.example.com -t 'plant/line-3/temp' -m '72.4'

The subscriber prints plant/line-3/temp 72.4. The -v flag prints the topic in front of the payload.

The broker treats the payload as raw bytes and does not parse it. JSON, Protobuf or a plain number are all fine.

Core responsibilities of a broker

The MQTT standard gives the broker several duties beyond routing.

Responsibility What the broker does
Authentication Checks the client’s identity, for example with username and password or a TLS client certificate
Authorization Decides which topics a client may publish or subscribe to, usually with access control lists (ACLs)
Sessions Stores subscriptions and undelivered messages for clients with a persistent session
QoS handling Sends and expects acknowledgements for QoS 1 and QoS 2 messages, and retries where needed
Retained messages Stores the last retained message per topic and sends it to new subscribers
Keep alive Detects dead connections when a client stops sending packets
Last Will Publishes a client’s Will message when the client disconnects without a clean DISCONNECT

Sessions and Clean Start

A session is the state the broker keeps for one client ID: its subscriptions and the QoS 1 and QoS 2 messages that are not yet delivered or acknowledged. In MQTT 3.1.1 the client controls this with the Clean Session flag. If it is set to 0, the session survives a disconnect. In MQTT 5.0 this is split into two settings: Clean Start decides whether an existing session is discarded on connect, and Session Expiry Interval decides how long the broker keeps the session after the connection closes.

Persistent sessions let a device that loses its cellular link receive the messages it missed once it reconnects. They also cost broker memory and storage, so the expiry interval should match how long a message is still useful.

Keep alive

Keep alive tells the broker how often to expect traffic from a client. If the client has nothing to send, it sends a PINGREQ and the broker answers with PINGRESP. If the broker receives nothing for one and a half times the keep alive interval, it closes the connection.

Last Will and Testament

The Last Will is a message the client registers when it connects. If the connection breaks without a proper DISCONNECT, for example because the device lost power, the broker publishes this message for the client. A common pattern is a retained offline status on a topic such as fleet/ev-0042/status, so every consumer can see that the vehicle has dropped off. MQTT 5.0 adds a Will Delay Interval, so a short reconnect does not trigger the Will.

Security: TLS, ports and access control

An MQTT broker should accept production traffic only over TLS. IANA registers port 1883 for MQTT over plain TCP and port 8883 for MQTT over TLS. On port 1883, usernames, passwords and payloads cross the network in clear text.

mosquitto_sub -h broker.example.com -p 8883 --cafile ca.crt \
  -u historian -P 'secret' -t 'plant/#'

TLS protects the connection. ACLs protect the data. A temperature sensor on line 3 usually needs to publish only to its own topics, and a dashboard usually needs to subscribe only. Restricting each client to the topics it needs limits the damage if one device is compromised.

Broker features outside the standard

Some important broker properties are not defined by MQTT at all. The standard describes the protocol between a client and one server. It does not define clustering, high availability, how the broker stores data on disk, bridges between brokers, or integration with databases and stream platforms. These are implementation choices, and they differ between broker products.

When you evaluate a broker, check these points against your own requirements:

  • Supported protocol versions (3.1.1, 5.0) and transports (TCP, TLS, WebSocket)
  • How authentication and ACLs are configured and where they are stored
  • Behaviour when a node fails: are sessions and retained messages preserved?
  • Limits on connections, message size, queued messages per session and retained messages
  • Monitoring: many brokers publish statistics under $SYS/ topics, but their content is not standardized

When to use an MQTT broker

An MQTT broker fits well when many devices send small, frequent messages over networks that may be slow or unreliable, and several applications need the same data. Typical cases are machine and sensor data in factories, telemetry from vehicles and charging stations, and status data from robots, for example robot/arm-7/joint-3.

It is a poor fit for large file transfers and for long-term storage or replay of history. A broker is not a database. Systems that need replay forward MQTT data into a historian, a time-series database or a log-based platform.

01ultramq/learn/faq

Frequently asked questions

What is the difference between an MQTT broker and an MQTT client?

The broker is the server that receives and distributes messages. A client is any device or application that connects to the broker to publish messages, subscribe to topics, or both.

Which port does an MQTT broker use?

Port 1883 is registered for unencrypted MQTT over TCP and port 8883 for MQTT over TLS. Production systems should use TLS on 8883.

Do MQTT clients talk to each other directly?

No. All messages go through the broker. A publisher does not know who receives its message, and a subscriber does not know who sent it.

Can an MQTT broker store messages for offline clients?

Yes, if the client uses a persistent session. The broker then keeps the client's subscriptions and queues QoS 1 and QoS 2 messages until the client reconnects or the session expires.

03ultramq/start

Talk to an engineer about your project.

Tell us what you want to connect, where it has to run and what the data should feed. We reply by email.

SUBultramq/#