MQTT Persistent Session Not Restoring Subscriptions
DodaTech
Updated 2026-06-26
1 min read
In this tutorial, you'll learn about MQTT Persistent Session Not Restoring Subscriptions. We cover key concepts, practical examples, and best practices.
The Problem
After reconnection, persistent session does not restore previous subscriptions.
Quick Fix
Wrong
client.connect({ clean: true }) # Clean session clears everything
All subscriptions and queued messages lost on every reconnect.
Right
// MQTT 3.1.1 — persistent session
const client = mqtt.connect('mqtt://broker:1883', {
clientId: 'my-fixed-client-id', // MUST be same
clean: false, // Persist session
reconnectPeriod: 5000
})
client.on('connect', () => {
// Subscriptions are restored by broker if session exists
// But safe to subscribe anyway (broker ignores duplicates)
client.subscribe('my/topic', { qos: 1 })
})
// Check if session was present
client.on('connect', (connack) => {
if (connack.sessionPresent) {
console.log('Session restored from previous connection')
}
})
Session persists across reconnects — subscriptions and queued messages preserved.
Prevention
Use fixed Client ID (same each connection). Set clean = false. The broker returns sessionPresent = true if the session was found. Queue offline messages at QoS 1/2. Session state includes: subscriptions, queued messages, and will message. Session lifetime is configured on the broker (default 2h on EMQX).
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro