Skip to content

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.
// 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

### What state is stored in a session?

Subscriptions, pending QoS 1/2 messages, will message. Not current connection state or active publications.

How long does the broker keep the session?

Configurable by broker. EMQX default: 2 hours after disconnect. MQTT 5.0: Session Expiry Interval property.

What happens if multiple clients use the same ID?

The previous session is taken over. The earlier connection is disconnected with a Session Taken Over reason.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro