project
An MQTT client that survives losing the network
A SQLite offline queue, a separate inflight tracker for QoS 1/2, and priority eviction that logs every drop. Soak-tested for 24 hours.
The question this answers is what happens to your data when the network drops. Most MQTT client examples assume the broker is always reachable. On a Pi on a factory floor or a remote sensor node, that assumption fails regularly.
The client is built around dual storage. An offline queue in SQLite holds messages when the broker is unreachable and drains on reconnect. A separate inflight tracker persists QoS 1 and 2 messages across reconnects until the broker acknowledges them — genuinely two different problems needing two tables. On recovery, inflight messages from the previous session go first (the broker may still hold their packet IDs), then the offline queue drains in priority order on a background thread.
Priority eviction means the queue never silently fills: at capacity, lower-priority messages are dropped for higher-priority ones and every eviction is logged. Silent data loss is worse than visible data loss.
The full production version is deployed at a commercial facility and is company IP. This is a representative open-source build showing the core architecture.
What I took away
SQLite’s threading restrictions fail silently — the connection doesn’t complain at
creation, only on access from the wrong thread, and the error doesn’t point at the cause.
check_same_thread=False plus explicit locking is the right pattern.
Priority eviction only adds value if it’s deterministic and observable. A policy that drops data quietly is indistinguishable from a bug.
Exponential backoff needs a guard flag to be correct in practice. The MQTT disconnect callback can fire several times for one failure, and without the guard the backoff doubles per callback rather than per reconnect attempt.