MQTT 3.1.1 is the most widely adopted version of the MQTT protocol, standardized by OASIS in 2014. HiveMQ CE provides full support for both MQTT 3.1 and 3.1.1, ensuring compatibility with the vast majority of MQTT clients and devices.
MQTT 3.1.1 refined and clarified the MQTT 3.1 specification without introducing breaking changes. HiveMQ CE supports both versions seamlessly.
Retained messages are stored by the broker and delivered to new subscribers immediately upon subscription.
# Publish a retained messageclient.publish("device/status", "online", retain=True)# New subscribers immediately receive the last retained messageclient.subscribe("device/status")
Use cases:
Last known device status
Configuration values
System state information
To delete a retained message, publish an empty message with the retain flag set to the same topic.
When clean_session is set to False, the broker maintains:
The client’s subscriptions
QoS 1 and QoS 2 messages not yet acknowledged
New QoS 1 and QoS 2 messages while disconnected
# Create a persistent sessionclient = mqtt.Client(client_id="device123", clean_session=False)client.connect("localhost", 1883, 60)# Subscribe with QoS 1client.subscribe("commands/device123", qos=1)# Even if disconnected, messages are queued by HiveMQ CE
HiveMQ CE stores queued messages in persistent storage (RocksDB) to survive broker restarts.
LWT allows clients to specify a message that the broker will publish if the client disconnects ungracefully.
import paho.mqtt.client as mqttclient = mqtt.Client()# Set Last Will messageclient.will_set( topic="devices/device123/status", payload="offline", qos=1, retain=True)client.connect("localhost", 1883, 60)
Maximum length: 65535 bytes (configurable in HiveMQ CE)
Hierarchical structure using / as delimiter
Case-sensitive
Best practices:
✅ Good topic structure:home/livingroom/temperaturehome/bedroom/humidityfactory/line1/machine5/status❌ Avoid:/leading/slash/topic # Leading slash adds empty leveltopic//double//slash # Empty levelstopic with spaces # Spaces can cause issues
# Subscribe to temperature sensors in all roomsclient.subscribe("home/+/temperature")# Matches:# ✅ home/livingroom/temperature# ✅ home/bedroom/temperature# ❌ home/livingroom/humidity# ❌ home/bedroom/sensors/temperature
Matches one or more topic levels (must be last character):
# Subscribe to all topics under homeclient.subscribe("home/#")# Matches:# ✅ home/livingroom/temperature# ✅ home/bedroom/humidity# ✅ home/livingroom/sensors/temp1# ✅ home
Avoid subscribing to # (all topics) in production as it can cause performance issues.