Czy wolisz polską wersję strony elektroda?
Nie, dziękuję Przekieruj mnie tamHow to make MQTT to send values after the MQTT broker has restarted
• Publish every “state” message with the retain flag set and (preferably) QoS 1.
• Keep broker persistence enabled so the retained database survives a restart.
• Let clients reconnect automatically (fixed ClientID, cleanSession=false/cleanStart=false).
With these three measures the broker will send the last stored value to every client immediately after both sides are back online—no manual re-publishing is required.
cleanSession = false; MQTT 5 cleanStart = false, sessionExpiryInterval > 0)| Failure moment | Retain | Persistent session | Result |
|---|---|---|---|
| Broker dies before PUBACK | Irrelevant (retain not stored) | Client resends after reconnect, so value finally stored | |
| Broker dies after PUBACK | Retain already stored on disk, survives | Nothing to redo | |
| Subscriber reconnects | Broker immediately sends retained msg | Broker also drains queued QoS 1/2 messages |
• MQTT v5 adds sessionExpiryInterval (fine-grained control instead of boolean cleanSession).
• Message properties messageExpiryInterval prevent stale data buildup in retained DB.
• Modern brokers (EMQX 5.x, HiveMQ 4.x, Mosquitto 2.0) persist the store with LMDB or RocksDB for GB-scale datasets.
• Edge devices increasingly include on-device flash buffering (Azure IoT SDK, AWS Greengrass, Zephyr RTOS “settings” backend) so that a publisher can ride out multi-hour outages.
Mosquitto CLI
mosquitto_pub -t "plant/temperature" -m "23.8" -q 1 -r
Python / Paho
client.publish("plant/temperature", payload="23.8", qos=1, retain=True)
Enable persistence (Mosquitto)
persistence true
persistence_file mosquitto.db
persistence_location /var/lib/mosquitto/
autosave_interval 30 # s
Robust client skeleton (Paho)
import paho.mqtt.client as mqtt
client = mqtt.Client(client_id="sensor-001", clean_session=False)
client.reconnect_delay_set(min_delay=1, max_delay=120)
client.will_set("plant/status", "offline", qos=1, retain=True)
def on_connect(c,u,f,rc): if rc==0: c.publish("plant/status", "online", qos=1, retain=True)
client.connect("broker.lan", 1883, 60) client.loop_start()
4. Clearing a wrong retained value
mosquitto_pub -t "plant/temperature" -r -n
---
### Ethical and legal aspects
• Retained data can unintentionally expose historical or private sensor values.
• In medical / safety-critical fields ensure compliance with IEC 62304, FDA CFR 820 (software recall of stale data).
• GDPR/CCPA may classify retained user data as *personal data*; provide a deletion mechanism.
---
### Practical guidelines
• Always give each device a **stable ClientID**; otherwise the broker cannot link the new connection with its old session.
• For high-rate telemetry consider a “state topic” (retained) plus a “live stream topic” (non-retained) to avoid disk bloat.
• Validate persistence by:
1) Publish retained value.
2) `systemctl restart mosquitto`.
3) `mosquitto_sub -t "plant/#"` – the value should pop up instantly.
• If your client library only buffers in RAM and the device might reboot, add a local flash queue (SQLite, JSON file).
---
### Possible disclaimers or additional notes
• Retained messages are **not** a historical queue—only the last value is kept. For full history use MQTT v5 Shared Subscriptions with an external database or time-series system (InfluxDB, TimescaleDB).
• QoS 2 adds latency; use it only when duplicate suppression is indispensable.
• Disk persistence can be disabled on embedded brokers (memory constrained); confirm the default.
---
### Suggestions for further research
• Evaluate MQTT-SN or Sparkplug-B if you run sensor networks with sleep cycles.
• Look at “bridging” two brokers with inter-broker queues for geographic redundancy.
• Explore NTS 1.1 (NATS JetStream) or Apache Pulsar for scenarios requiring replay of the entire message stream.
• Read the MQTT v5.0 specification sections 3.3 (PUBLISH) and 3.1.2.4 (Session Expiry Interval) for deeper protocol insight.
---
### Brief summary
Set the **retain flag**, keep **broker persistence** enabled, and make clients use **persistent sessions with auto-reconnect**. After a broker restart, the broker reloads the retained database from disk and, upon each client’s reconnection, instantly republishes the last stored value, achieving seamless state recovery with no lost data.