You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(realtime): wait for postgres_changes system ready before emitting SUBSCRIBED - #2029
Fixes a race condition where SUBSCRIBED status is emitted before PostgreSQL logical replication is ready,
causing early database writes to miss postgres_changes events.
Problem
The Realtime subscription has a two-stage backend process:
Stage 2 (Slow): PostgreSQL replication slot creation and stream initialization
Previously, the client emitted SUBSCRIBED after Stage 1, but the replication stream wasn't ready until Stage 2 completed. Any database writes in this window (~1-3 seconds) would not trigger postgres_changes events.
Solution
Wait for the server's system message { extension: 'postgres_changes', status: 'ok' } before emitting SUBSCRIBED when postgres_changes bindings are present. This message is sent by the server after Realtime.Tenants.ReplicationConnection confirms the replication stream is active.
For channels without postgres_changes (broadcast/presence only), behavior is unchanged.
Changes
Store subscribe callback and track pending system confirmations
Add _maybeEmitSubscribed() that only fires when all confirmations received
Handle system message in _trigger() to clear pending state
Updated existing lifecycle tests to include system message trigger.
Hey @mandarini, funnily enough we were discussing this issue last week and I think we found a good way forward here that is good enough:
We think that we could change so that IF a channel has ONLY postgres changes (so no broadcast and presence on callbacks) we could then only consider SUBSCRIBED then the system ready message arrives.
Hey @mandarini, funnily enough we were discussing this issue last week and I think we found a good way forward here that is good enough:
We think that we could change so that IF a channel has ONLY postgres changes (so no broadcast and presence on callbacks) we could then only consider SUBSCRIBED then the system ready message arrives.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a race condition where
SUBSCRIBEDstatus is emitted before PostgreSQL logical replication is ready,causing early database writes to miss
postgres_changesevents.Problem
The Realtime subscription has a two-stage backend process:
Previously, the client emitted
SUBSCRIBEDafter Stage 1, but the replication stream wasn't ready until Stage 2 completed. Any database writes in this window (~1-3 seconds) would not triggerpostgres_changesevents.Solution
Wait for the server's system message
{ extension: 'postgres_changes', status: 'ok' }before emittingSUBSCRIBEDwhen postgres_changes bindings are present. This message is sent by the server afterRealtime.Tenants.ReplicationConnectionconfirms the replication stream is active.For channels without postgres_changes (broadcast/presence only), behavior is unchanged.
Changes
_maybeEmitSubscribed()that only fires when all confirmations received_trigger()to clear pending stateRelated