MQTT GUIDE
3 min readMQTT Session and Message Expiry
MQTT 5.0 adds two expiry controls. The Session Expiry Interval sets how long the broker keeps a disconnected client’s session — its subscriptions and queued messages — before discarding it. The Message Expiry Interval sets how long an individual message stays valid while queued; if it expires before it can be delivered, the broker drops it.
MQTT 5.0 gives you two independent expiry controls that MQTT 3.1.1 lacked: how long a disconnected client’s session is kept, and how long an individual message stays valid. Together they stop the broker from hoarding stale state and stale data.
Session Expiry Interval
When a client connects, it can set a Session Expiry Interval — the number of seconds the broker should keep its session after it disconnects. Reconnect within that window and the broker restores your subscriptions and delivers whatever queued up while you were away. Miss it, and the session (and its queue) is discarded. Setting it to zero means “clean up as soon as I disconnect”.
Message Expiry Interval
A publisher can stamp an individual message with a Message Expiry Interval — how long it stays worth delivering. If the message is still queued for an offline subscriber when the interval runs out, the broker drops it rather than delivering something out of date. When the broker does deliver a delayed message, it decrements the remaining interval by the time the message spent waiting, so downstream clients see an honest time-to-live.
Expiry in TBMQ
TBMQ honors both intervals. It caps the session expiry interval at one week, so a forgotten client can’t hold a session forever, and when it delivers a queued message it reduces the message’s remaining expiry by the time it waited in the broker. See the clean vs persistent sessions guide for how this ties into session handling.
Frequently asked questions
What is the Session Expiry Interval in MQTT 5.0?
A value the client sends at connect that tells the broker how many seconds to keep the session — subscriptions and queued messages — after the client disconnects. Zero means discard the session immediately on disconnect; a large value keeps it for a long time. It replaces MQTT 3.1.1’s all-or-nothing clean-session flag.
What is the Message Expiry Interval?
A per-message lifetime in seconds. If a message is still waiting to be delivered when its interval elapses, the broker discards it instead of delivering something stale. When the broker does deliver a delayed message, it lowers the remaining interval by the time the message spent waiting.
What is the difference between session expiry and message expiry?
Session expiry bounds how long the whole session (subscriptions plus its message queue) survives after the client goes offline. Message expiry bounds how long a single message stays valid. A session can outlive individual messages inside it.
How did MQTT 3.1.1 handle this?
It didn’t, really. MQTT 3.1.1 had only the clean-session flag: either the session was dropped immediately on disconnect, or kept indefinitely. There was no per-message expiry at all.
What happens if I set session expiry to zero?
The session ends the moment the client disconnects — subscriptions and any queued messages are dropped. This is the MQTT 5.0 equivalent of a clean session, useful for clients that never need to resume state.
Run it yourself
TBMQ is a free, open-source MQTT broker built to scale. Spin it up in minutes or try the live demo — no install required.