Explainer
Why an MQTT subscriber stops receiving after a broker restart
Short answer
After a broker restart, an MQTT client can reconnect without restoring its subscription: Connected shows TRUE while the broker holds no topic filter for the new session. In Automation Studio 6.5.2.12 with Runtime 6.6.3 on an X20CP0483, a packet capture confirmed no new SUBSCRIBE after reconnect. The fix is one false-to-true cycle of IotMqttSubscribe.Enable on the reconnect transition.
On this page
The subscriber goes quiet because a reconnect restores the connection but not the subscription. MQTT treats those as separate objects: the topic filters belong to the session, and a session that starts fresh after a broker restart carries none, so arriving messages match nothing and are never forwarded. In Automation Studio 6.5.2.12 with Automation Runtime 6.6.3 on an X20CP0483 controller, a packet capture confirmed that the client reconnects and sends no new SUBSCRIBE. The fix is to cycle IotMqttSubscribe.Enable from FALSE to TRUE once, on the false-to-true transition of IotMqttClient.Connected.
Connected proves the connection, not the subscription #
The path from a publisher to your PLC has four distinct states, and each one can fail alone:
- The TCP connection to the broker's address and port.
- The MQTT session, opened by CONNECT and confirmed by CONNACK.
- The subscription, registered by SUBSCRIBE and confirmed by SUBACK.
- Message delivery of publications that match the registered filter.
Connected reflects state 2 and nothing more. When the broker restarts, the client re-runs CONNECT/CONNACK and the flag returns TRUE, but state 3 is a separate transaction that nobody re-triggered. This is the same class of false positive described in why MQTT Engine shows online status after an edge node drops: the status bit reports one layer while the data path depends on another.
A working publisher proves even less, because publish and subscribe operations run on their own internal states. Only the subscriber's own capture shows whether its registration exists.
What the MQTT standard requires #
MQTT v3.1.1, the OASIS standard, stores subscriptions as session state, and the connect flags decide what happens to that state:
- With CleanSession set to 1, client and broker must discard any previous session at CONNECT. The subscriptions are gone by definition, and the client is expected to subscribe again after it reconnects.
- With CleanSession set to 0, the broker must retain the subscriptions as long as the session exists, but the standard does not require a broker to survive its own process restart with sessions intact. Keeping them across a restart is broker-specific persistence to disk, not protocol-guaranteed behavior.
MQTT v5.0 replaces the single flag with Clean Start and a Session Expiry Interval, and the restart case ends the same way: without broker-side persistence, the new session holds no filters. This is also why Sparkplug defines an explicit rebirth sequence for its nodes, covered in when to use the Sparkplug rebirth tag on an edge node.
Where the packet path stops #
Check each hop independently against a capture filtered to the traffic between the PLC and the broker address. For the case above, the path passes everything up to the subscription and stops there:
| Hop | What proves it | Observed after the broker restart |
|---|---|---|
| Network path | Traffic reaches the broker host | Pass |
| Transport | New TCP handshake on the configured port | Pass |
| MQTT connection | CONNECT followed by CONNACK | Pass, Connected goes TRUE |
| Subscription | SUBSCRIBE followed by SUBACK | Fail: no SUBSCRIBE sent |
| Delivery | A matching publication arrives | Fail, consequence of the missing subscription |
The decisive reading is therefore never Connected alone. It is either a resumed session the broker retained, or a fresh SUBSCRIBE/SUBACK exchange after the reconnect. If SUBSCRIBE appears but no SUBACK follows, look at broker logs and access rules; compare the filter with the publisher's topic byte for byte only after the broker has acknowledged the post-restart subscription.
The edge-triggered resubscribe #
In this configuration, IotMqttSubscribe.Enable is the command that starts the subscription state machine. When it stays TRUE across a disconnect and reconnect, the block returns to a connected state without starting a new subscription transaction. Cycling it low then high produced this exchange on the wire:
UNSUBSCRIBE
UNSUBACK
SUBSCRIBE
SUBACK
and message reception resumed immediately. The recovery procedure:
- Record the previous state of
IotMqttClient.Connectedand detect its false-to-true transition after a connection loss. - On that transition, set
IotMqttSubscribe.Enableto FALSE. - Wait one task cycle so the block processes the disable.
- Set
IotMqttSubscribe.Enableback to TRUE once. - Confirm SUBSCRIBE and SUBACK in the capture, then publish a test message on the exact configured filter and confirm the PLC receives it.
- Restart the broker again and verify exactly one recovery cycle runs for the new reconnect.
Trigger on the edge, never on the level. If the cycle runs whenever Connected is TRUE, the subscription is torn down and rebuilt every scan and the broker sees a continuous unsubscribe/subscribe storm.
How long the gap lasts #
The outage-to-recovery time has two parts: noticing the loss, and re-registering the filter. A worked example:
- Keepalive configured: 60 s. The MQTT grace period is 1.5 times the keepalive, so a peer that vanishes silently is detected after at worst 1.5 x 60 s = 90 s. A clean broker stop usually closes the TCP connection, and detection is then immediate.
- PLC task cycle: 10 ms. The recovery takes four cycles from the transition: one to write FALSE, one for the block to execute the disable, one to write TRUE, and one to issue SUBSCRIBE. That is 4 x 10 ms = 40 ms, plus one broker round trip for SUBACK.
Worst case, the subscription is restored 90 s + the broker's reconnect time + about 40 ms after the broker stops; typical case, reconnect time plus 40 ms. Shorten the worst case by lowering the keepalive, at the cost of more keepalive traffic.
The decision for each case #
- Clean session (CleanSession = 1, or Clean Start in v5): the subscription cannot survive the restart by design. Application-controlled resubscription on the reconnect transition is mandatory. This is the stated configuration and the correct default.
- Persistent session that you have verified survives a broker restart: the broker persists sessions to disk and a capture proves messages flow after restart with no new SUBSCRIBE. You may rely on it, but retest after every broker upgrade or configuration change, because the standard does not promise it.
- Neither resubscribe logic nor verified persistence: the subscription dies at the first restart and never returns. Not acceptable for a plant data path.
Commit to the edge-triggered resubscribe as the default design: it depends only on your own code, works on any conforming broker, and costs about 40 ms per reconnect in the example above. Use broker-side session persistence only as a verified optimization on top of it, never instead of it.
Questions asked
Is manual resubscription required with this MQTT library?
Yes. For Automation Studio 6.5.2.12 with Runtime 6.6.3 on an X20CP0483, no built-in mechanism restored the subscription after a broker restart, and the capture showed no automatic SUBSCRIBE. Treat application-level resubscription as mandatory unless you have verified that the broker retains the session across its own restart.
How long can the client take to notice the broker died?
The MQTT grace period is one and a half times the keepalive. With a keepalive of 30 seconds, a silent peer can go unnoticed for up to 45 seconds, which is the worst-case detection time. A clean broker stop usually closes the TCP connection immediately, so detection is often instant; the keepalive bound covers a host that vanishes without closing the socket.
Can the broker keep subscriptions across its own restart?
Only when it writes session state to disk and the client reconnects with a continuing session. MQTT v3.1.1 does not require a broker to survive a restart with sessions intact, so this is broker configuration, not protocol behavior. Verify it with a capture: after the restart, messages must flow with no new SUBSCRIBE.
Why does the fix send UNSUBSCRIBE before SUBSCRIBE?
The falling edge of IotMqttSubscribe.Enable makes the block remove its previous filter on the live connection, producing UNSUBSCRIBE and UNSUBACK. The rising edge then starts the subscription state machine again, producing SUBSCRIBE and SUBACK. The extra exchange is harmless and confirms the state machine restarted cleanly.