MQTT · Study deck

MQTT Packet and Broker Features

Picture a building sensor that publishes an alarm just before its receiver disconnects.

Broker Bex is your guide for this deck.

fund
Broker Bex, the module guide, in a scene from this chapter.
iotclass.org

After studying this chapter

Learning objectives

You will be able to:

  • Analyze MQTT Packet Structure: Decode binary MQTT packets field by field and calculate exact byte overhead for given topic lengths and QoS levels
  • Design Topic Hierarchies: Construct scalable, maintainable topic naming conventions that support wildcard queries for large device fleets
  • Calculate Bandwidth Savings: Apply optimization techniques — short topic names, binary payloads, topic aliases — and quantify savings across a device fleet
  • Implement MQTT 5.0 Features: Configure message expiry, topic aliases, and shared subscriptions in code and explain when each feature is appropriate
iotclass.org

Major section

In 60 Seconds

After reconnection, a new message, an old retained value, and a queued delivery can look alike unless each carries clear identity and time.

  • Bandwidth means how much data a path can carry in a given time.
  • A broker is the service that routes messages by topic.
  • Telemetry means measurements and status sent from a remote device for review.

Key terms

MQTT
MQTT means Message Queuing Telemetry Transport.
iotclass.org

Major section

In 60 Seconds (continued)

MQTT means Message Queuing Telemetry Transport.

  • Quality of service means the selected delivery level.
  • QoS is its short name.
  • This runway does not prove fleet capacity or exactly-once physical action.
  • The deeper sections cover packet fields, topic cost, retained values, sessions, delivery choices, service sizing, and failure tests.
iotclass.org

Major section

MQTT's Secret Features

So when a new phone app connects at midnight, it instantly sees '22 degrees' instead of waiting until my next reading.".

  • Lila the LED shared her discovery: "And I set up a Last Will message!
  • "My favorite," said Bella the Battery, "is clean session = false.
  • Last will detects failures.

Why it matters

Retained messages prevent stale data.

iotclass.org

Major section

Designing Topic Hierarchy for Smart Building

Each floor has 20 rooms with temperature sensors, occupancy detectors, and smart lighting.

  • End by: Access patterns: Dashboard shows all temps, HVAC controls one floor.
  • The first three levels locate the source, the device-type level separates families such as sensors and lights, and the final level states what is measured or commanded.

Why it matters

Physical hierarchy (building/floor/room) enables location-based queries.

Smart building MQTT topic hierarchy showing building, floor, room, device type, and measurement topic levels with example wildcard subscriptions.
Smart building MQTT topic hierarchy showing building, floor, room, device type, and measurement topic levels with example wildcard subscriptions.
iotclass.org

Major section

Deep-Dive Note: Packet Evidence and Retained State

Advanced MQTT features are easiest to debug when packet evidence and broker state stores are kept separate.

  • The fixed header's first byte identifies the control packet, and for PUBLISH the low nibble carries DUP, the two QoS bits, and RETAIN.

Why it matters

That evidence prevents two common misdiagnoses.

Retained messages are broker-side topic state: the publisher marks one PUBLISH as retained, the broker stores it as the current value for that topic, and a later subscriber receives it immediately.
Retained messages are broker-side topic state: the publisher marks one PUBLISH as retained, the broker stores it as the current value for that topic, and a later subscriber receives it immediately.
iotclass.org

Major section

Deep-Dive Note: Packet Evidence and Retained State (continued)

A quiet client can prove it is alive with 0xC0 0x00 (PINGREQ, zero remaining bytes), while a retained QoS 1 publish might start with 0x33: packet type 3, DUP=0, QoS=01, and RETAIN=1.

  • A useful packet note records the raw first byte, decoded flags, Remaining Length bytes, topic length, topic string, packet identifier when QoS is above 0, and payload length.
  • Topic-name shortening and MQTT 5 topic aliases save bytes in the variable header, but they do not change QoS, retain, or session behavior.
  • That evidence prevents two common misdiagnoses.
iotclass.org

Deck summary

Key takeaways

After reconnection, a new message, an old retained value, and a queued delivery can look alike unless each carries clear identity and time.

  • MQTT means Message Queuing Telemetry Transport.
  • So when a new phone app connects at midnight, it instantly sees '22 degrees' instead of waiting until my next reading.".
  • Each floor has 20 rooms with temperature sensors, occupancy detectors, and smart lighting.
  • Advanced MQTT features are easiest to debug when packet evidence and broker state stores are kept separate.
iotclass.org

Retrieval practice

Recall check 1 of 3

Broker Bex says: answer from memory, then check your reasoning.

Q1You're designing an MQTT topic structure for a fleet of 500 delivery trucks. Each truck has GPS, fuel level, and engine temperature sensors. Which topic hierarchy is most appropriate?

Afleet/truck-001/gps, fleet/truck-001/fuel, fleet/truck-001/temp
Bgps/truck-001, fuel/truck-001, temp/truck-001
Ctruck-001-gps, truck-001-fuel, truck-001-temp (flat topics)
Dfleet/sensors/truck-001/gps with JSON payload containing all data
Show answer

Answer: A Correct!

iotclass.org

Retrieval practice

Recall check 2 of 3

Broker Bex says: answer from memory, then check your reasoning.

Q2An IoT sensor publishes temperature readings every 10 seconds over a cellular link with a 42-byte topic name. After upgrading to MQTT 5.0, the developer enables topic aliases. How much bandwidth is saved per message after the alias is established, and which MQTT 3.1.1 feature could NOT be replaced by a topic alias?

A40 bytes saved per message (42-byte topic minus 2-byte alias); shared subscriptions cannot be replaced
B42 bytes saved per message (entire topic eliminated); message expiry cannot be replaced
C44 bytes saved per message (topic plus 2-byte length field); retained messages cannot be replaced
DNo bandwidth saved; topic aliases only reduce broker CPU usage, not wire size
Show answer

Answer: A Correct!

iotclass.org

Retrieval practice

Recall check 3 of 3

Broker Bex says: answer from memory, then check your reasoning.

Q3Place each MQTT role where it lives so you can distinguish client intent, broker-held state, and what subscribers receive after connect or failure.

AMQTT Broker (Session + Retained Store)
BCoAP Server
CREST Gateway
DAMQP Broker
Show answer

Answer: A Separate publishers, broker state, and consumers so you can predict retained delivery and Last Will behaviour without confusing their owners.

Q4Complete the MQTT publisher with retained messages and Last Will and Testament:

Aclient.will_set('gateways/gateway_01/status', 'offline', qos=1, retain=True)
Bclient.set_will('gateways/gateway_01/status', 'offline', qos=1)
Cclient.lwt('gateways/gateway_01/status', 'offline', retain=True)
Dclient.will_message('gateways/gateway_01/status', 'offline')
Show answer

Answer: A will_set() pre-registers a message the broker publishes if this client disconnects unexpectedly.

iotclass.org

Print reference

Answers

Answer key.

  1. A · Correct!
  2. A · Correct!
  3. A · Separate publishers, broker state, and consumers so you can predict retained delivery and Last Will behaviour without confusing their owners.
  4. A · will_set() pre-registers a message the broker publishes if this client disconnects unexpectedly.
iotclass.org