DEV Community

Nwajichukwu Goodness
Nwajichukwu Goodness

Posted on

Your Realtime Channel Is ATTACHED. But Did You Actually Recover Every Message?

Your Realtime Channel Is ATTACHED. But Did You Actually Recover Every Message?

In a realtime application, reconnecting successfully does not necessarily mean the application has received every message it missed.

This distinction matters when building chat applications, collaborative interfaces, live dashboards, notification systems, and other products that depend on continuous message delivery.

Consider a typical implementation using a realtime Pub/Sub SDK.

A client connects to a service, subscribes to a channel, receives messages, loses its network connection, and eventually reconnects.

The SDK handles reconnection and attempts to restore the previous channel subscription.

The channel becomes "ATTACHED" again.

At this point, there is an important question:

Did the client recover its previous message position, or did it simply establish a new attachment?

These are not necessarily the same thing.

  1. Understanding the implementation path

A typical realtime message flow looks like this:

  1. The client establishes a connection.
  2. The application subscribes to a channel.
  3. The client receives messages.
  4. The network connection drops.
  5. The SDK attempts connection recovery.
  6. The client reconnects and the channel reattaches.
  7. The application determines whether message continuity was preserved.
  8. If continuity was lost, the application recovers and reconciles missed messages.

The important implementation detail is step seven.

A successful channel attachment alone does not establish that the client has received every message published during its absence.

  1. What does ATTACHED actually establish?

In Ably Pub/Sub, "ATTACHED" indicates that a channel attachment has succeeded.

However, Ably also exposes a "resumed" property through the channel state change object.

This property provides additional information about message continuity.

There are two important scenarios.

Scenario A: Successful recovery

The client reconnects and Ably successfully resumes the previous channel position.

The "resumed" property is "true".

This indicates that message continuity was preserved.

Scenario B: Attachment without continuity

The previous channel position cannot be fully recovered.

The channel can still become attached, but "resumed" is "false".

This means the application cannot assume that every message has been received.

There is also an important distinction:

When a channel is attached for the first time on a fresh connection, "resumed" is normally "false" because there is no previous attachment to resume.

Therefore, an application should not interpret every initial "resumed: false" event as a recovery failure.

It needs to consider the previous connection and attachment context.

  1. Connection recovery has a time boundary

Ably maintains connection state for up to two minutes following a disconnection.

Within this recovery window, its SDKs automatically attempt to resume the connection and deliver missed messages.

However, after the recovery window expires, the previous connection state can no longer be resumed.

The application then needs another recovery strategy.

The two-minute boundary is not the only consideration.

Ably also documents other continuity-loss scenarios, including:

  • "90003": Channel continuity cannot be guaranteed.
  • "90004": The channel backlog exceeds the replay limit.
  • "90005": The channel resumes in a different region.

These situations demonstrate why reconnecting should not be treated as sufficient evidence of complete message recovery.

  1. Continuity can also be lost while the channel remains attached

Another important detail is the "UPDATE" event.

A developer might monitor only "ATTACHED" events when implementing recovery logic.

However, Ably documents that partial continuity loss can also be communicated through an "UPDATE" event with "resumed: false".

This means the channel can remain attached while the application is notified that message continuity has been lost.

Consequently, recovery logic should account for both:

  • "ATTACHED" events with "resumed: false".
  • "UPDATE" events with "resumed: false".

Ignoring the second scenario can leave an application unaware of a documented continuity-loss condition.

  1. Recovering messages using channel history

Once a discontinuity is detected, the application needs to determine which messages it has missed.

Ably provides a History API for retrieving previously published messages.

One useful option is:

"untilAttach: true"

This allows the application to retrieve message history up to the point at which the channel was attached.

A simplified JavaScript example:

const history = await channel.history({
untilAttach: true
});

The result is paginated.

The application may need to retrieve multiple pages depending on how much history it needs.

There is another important consideration.

"untilAttach" establishes the upper boundary of the history request. It does not automatically determine how far back the application should retrieve messages.

The application must establish that boundary itself.

One practical approach is to persist the serial of the last message successfully processed.

That gives the application a reference point when determining which historical messages it still needs.

  1. History is not a replacement for application persistence

There is a responsibility boundary worth understanding here.

Ably's History API provides message retrieval, but it is not intended to replace a dedicated application database.

Ably explicitly recommends using a dedicated database for long-term persistence and more complex application queries.

A production implementation may therefore involve several responsibilities:

Component| Responsibility
Realtime SDK| Connection management and automatic recovery
Channel state monitoring| Detecting continuity loss
Message serial tracking| Establishing the last processed position
History API| Retrieving missed messages
Application database| Maintaining durable application state
Application logic| Reconciling recovered and already-processed messages

These responsibilities work together.

Retrieving historical messages does not automatically establish that the application's current state is correct.

For example, history results can overlap with messages already received through the live subscription.

The application must therefore account for duplicate processing.

Ably recommends designing message processing to tolerate duplicates, including tracking message IDs or serials.

  1. What developers should account for

When implementing a realtime integration, several questions deserve attention:

  • Does the application monitor both "ATTACHED" and "UPDATE" events?
  • Does it distinguish an initial attachment from a failed recovery?
  • Does it persist the last successfully processed message position?
  • Does it have a recovery strategy when continuity is lost?
  • Can it retrieve historical messages across multiple pages?
  • Can it reconcile historical messages with messages already received?
  • Does it maintain durable application state independently of realtime message delivery?

These questions help establish whether an application is merely reconnecting or actually restoring the state it needs.

Conclusion

Realtime connection recovery and application state recovery are related, but they are not identical responsibilities.

An SDK can manage reconnection, attempt to resume previous channel positions, and provide mechanisms for retrieving historical messages.

The application still needs to understand when continuity has been lost, determine what information is missing, retrieve the relevant history, and reconcile that information with its existing state.

A successful reconnection tells you that communication has been restored. It does not automatically establish that your application's message history is complete.

That distinction is important when designing reliable realtime systems.

Top comments (0)