Understand Connection State Recovery

Socket.IO supports connection state recovery, which can restore certain session details and missed packets after a temporary disconnection.

The feature is useful when a client loses connectivity briefly and reconnects within the configured recovery window.

However, recovery is not guaranteed in every situation. The server may be unable to recover a session after a process restart, expired recovery window, or unsupported adapter configuration.

Connection state recovery does not replace persistent application storage.

Important considerations:

  • Configure an appropriate recovery duration.
  • Decide whether middleware should be skipped during recovery.
  • Verify the recovered session and permissions.
  • Handle cases where recovery fails.
  • Persist important messages in a database.
Recovery Is Not Guaranteed

Connection state recovery is a best-effort feature. Do not use it as a replacement for storing important messages or business data in a database. The standard Redis adapter does not support connection state recovery.

Enable Connection State Recovery
import { Server } from 'socket.io';

const io = new Server(httpServer, {
  connectionStateRecovery: {
    maxDisconnectionDuration: 2 * 60 * 1000,
    skipMiddlewares: false
  }
});

Enables recovery for a two-minute window. Setting skipMiddlewares to false allows the connection middleware to run again during recovery.

Key Takeaways
  • Connection state recovery can restore certain session information.

  • Recovery is not guaranteed.

  • Persistent data must be stored independently.