Katman MQTT Client
Loading account

What Is MQTT? Clients, Brokers, and Topics

Learn how MQTT works, what clients and brokers do, and how topics, QoS, and retained messages fit together. Try a local publish/subscribe example.

By Katman

MQTT is a lightweight messaging protocol that lets devices and applications exchange data through a central server called a broker. A device publishes a message to a named topic, and the broker forwards it to clients subscribed to that topic.

For example, a temperature sensor can publish a reading once while a dashboard, a logger, and an alerting service each receive it. The sensor does not need a separate connection to every application that uses its data.

This guide explains that message flow, the settings you will encounter, and a small example you can run on your own computer.

How MQTT works

MQTT uses a publish/subscribe model. Three roles describe a typical exchange:

Role What it does Example
Publisher Sends a message to a topic A sensor sending a temperature reading
Broker Receives messages and routes them to matching subscribers An MQTT server such as Eclipse Mosquitto
Subscriber Requests messages matching a topic filter A dashboard displaying sensor readings

Publishers and subscribers are both MQTT clients. A single client can do both: a device might publish its temperature and subscribe to a topic for configuration commands.

A sensor client publishes a temperature reading to a broker, which forwards it to a dashboard and a logging client.

Clients establish connections to the broker. The broker handles routing, so the publishing client does not need to know which other clients are listening. MQTT commonly runs over TCP; some deployments use MQTT over WebSockets.

MQTT.org describes its use in areas including manufacturing, transportation, and smart homes. It is useful when several systems need updates from the same devices, especially when a compact messaging protocol suits their network and resource constraints.

Topics and payloads

A message has a topic name and a payload. The topic describes where the message belongs; the payload contains the data.

Topic:   lab/sensor-01/temperature
Payload: 22.4

The slash separates topic levels. You choose the naming convention for your application. For instance, factory/line-1/pump-07/status is easier to interpret than a collection of unrelated names.

Topic names are case-sensitive: lab/temperature and Lab/temperature are different topics. A subscriber can use a topic filter to match several names:

Filter Matches
lab/sensor-01/temperature That exact topic
lab/+/temperature One level between lab and temperature, such as sensor-01
lab/# lab and its descendant topics

The + wildcard occupies one complete level. The # wildcard matches the remaining levels and must be last. Wildcards belong in subscription filters; publish to a concrete topic name. These rules are defined in the MQTT 3.1.1 specification, section 4.7.

The payload is application data carried as bytes. Text and JSON are common choices, but MQTT does not require a JSON schema or decide what a value means. Your applications must agree on units, field names, and encoding. A reading of 22.4 needs a shared understanding that it represents, for example, degrees Celsius.

What QoS means

Quality of Service, or QoS, controls MQTT message delivery between a sender and a receiver:

Level Delivery behavior What to consider
QoS 0 At most once No MQTT acknowledgement or retry; a message may be lost
QoS 1 At least once Acknowledgements and retransmission can produce duplicates
QoS 2 Exactly once at the MQTT delivery layer Additional protocol exchanges prevent duplicate delivery on that hop

The publisher-to-broker and broker-to-subscriber deliveries are separate hops. A subscriber's granted QoS can limit the level used on its hop. The MQTT 5 specification, sections 3.8.4 and 4.3 explains these rules.

QoS does not confirm that a device performed a physical action. If you publish a pump command, a broker acknowledgement does not prove the pump started. For that, design an application reply and inspect the complete exchange. Our MQTT Request Response guide shows how to follow requests, replies, and timeouts.

Retained messages, sessions, and the Last Will

These features solve different problems:

  • Retained message: the broker stores the latest retained message for a topic and can send it to new matching subscribers. It is useful for a current state, such as a device's operating mode. It is not a history of every reading.
  • Persistent session: depending on the protocol version, session settings, QoS, and broker limits, subscriptions and queued messages can survive a disconnect. An ordinary new subscription does not automatically receive everything published before it existed.
  • Last Will: a client can arrange for the broker to publish a message when its connection ends unexpectedly, subject to the protocol's timing and session rules. Applications often use this to announce an offline state. It is not an immediate or complete diagnosis of a device failure.

For new examples, leave retain off unless you specifically want later subscribers to receive the value. Retaining a one-time device command can cause a newly connected subscriber to encounter an old instruction. The Mosquitto protocol guide explains retained messages and Wills; the MQTT 5 standard defines session expiry and the detailed protocol rules.

Try a local publish/subscribe example

You need Eclipse Mosquitto 2.x and its mosquitto_sub and mosquitto_pub command-line clients installed and available in your terminal. Use three terminals on the same computer. This example uses loopback networking, no credentials, and no TLS; keep it as a local exercise.

In the first terminal, start a broker:

mosquitto -p 18886

Mosquitto 2.x binds this command-line listener to loopback. Port 18886 is an example chosen for this lab; if another process uses it, choose a free port and change all three commands. See the Mosquitto broker manual.

In the second terminal, subscribe before publishing:

mosquitto_sub -h 127.0.0.1 -p 18886 -V mqttv5 -t "lab/+/temperature" -q 1 -v

In the third terminal, publish a reading:

mosquitto_pub -h 127.0.0.1 -p 18886 -V mqttv5 -t "lab/sensor-01/temperature" -m "22.4" -q 1

The subscriber should print:

lab/sensor-01/temperature 22.4

Here, -t selects the topic or filter, -q 1 requests QoS 1, and -V mqttv5 selects MQTT 5. The subscriber's -v option prints the topic as well as the payload. No retain flag is supplied, so the publication is not retained. Option details are in the subscriber and publisher manuals.

This exact exchange was checked with Mosquitto 2.0.22 on a local loopback broker. To see fan-out, start another subscriber with the same filter and publish again. To explore matching, publish on lab/sensor-02/temperature, then try lab/sensor-01/humidity; only the temperature topics match this filter.

Stop the clients and broker with Ctrl+C when you finish.

Where an MQTT client fits

The command-line tools make the message flow explicit. A graphical MQTT client helps you inspect topics and payloads while testing an application.

For the same exercise in Katman MQTT, connect to 127.0.0.1 on port 18886, choose MQTT 5.0, and subscribe to lab/+/temperature. Publish with the command above and inspect the arriving message. Katman is the client in this setup; Mosquitto remains the broker. If either runs in a VM or container, loopback addresses refer to that environment, so use a reachable broker address and the appropriate listener configuration instead.

Once a basic exchange works, Publish Templates help you reuse a test message. For a remote or production broker, use its authentication and TLS settings; our MQTT security and certificates guide explains the connection fields.

MQTT or HTTP?

Choose around the communication pattern. MQTT suits ongoing telemetry and events delivered to interested subscribers through a broker. HTTP often suits a direct request to a resource with a response, such as retrieving a configuration document or calling a web API. Many applications use both.

MQTT brings a broker to operate or obtain as a service, topic permissions to design, and connection/session behavior to understand. It is not automatically the best choice for every device or every network.

Start with one broker, one topic, and one observable message. Once you can follow the path from publisher to subscriber, the rest of the protocol's settings have a concrete purpose.