Skip to content

docs(realtime): add connect, subscribe, reconnect backoff and heartbeat specs - #304

Merged
grdsdev merged 4 commits into
mainfrom
claude/realtime-configuration-specs-1bc7bc
Nov 10, 2026
Merged

grdsdev merged 4 commits into
mainfrom
claude/realtime-configuration-specs-1bc7bc

Conversation

@grdsdev

@grdsdev grdsdev commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Adds Realtime specs for connection recovery:

  • realtime.client.connect: when the client reconnects, attempt counting (reset on socket open), no attempt limit, and the default full-jitter curve (base 1 s, cap 30 s). Applies to every SDK, whether or not it exposes the curve.
  • realtime.channel.subscribe: channel rejoin delays (steps 1/2/5/10 s, 30 s after a rate-limit join error), join timeout, and immediate rejoin on socket open. Not configurable.
  • realtime.configuration.reconnect_backoff: the optional caller-supplied attempt → delay function that replaces the default curve; negative delays count as zero.
  • realtime.configuration.heartbeat_interval: heartbeat cadence (a fixed beat, default 25 s), which replies count, and the missing reply → close → reconnect sequence. A missing reply is detected within two intervals of the last reply.

Context: supabase-swift v3 RealtimeClientOptions rework (SDK-2103). Swift v3 keeps the reconnect curve internal.

realtime.configuration.reconnect_backoff is planned for removal in a separate feat(realtime)!: PR, after the SDKs drop it from their sdk-compliance.yaml files. That PR deletes its spec too.

Known divergences, to file as supabase-js issues once this merges:

  • Default reconnect curve is stepped (1/2/5/10 s, then 10 s) with no jitter.
  • No 30 s rejoin wait after a rate-limit join error.
  • Schedules the next heartbeat one interval after the reply, not on a fixed beat.

@grdsdev
grdsdev requested a review from a team as a code owner October 29, 2026 15:34
@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Explain Change →

Caution

Review failed

The pull request is closed.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@grdsdev grdsdev changed the title docs(realtime): add reconnect backoff and heartbeat interval specs docs(realtime): add connect, subscribe, reconnect backoff and heartbeat specs Oct 29, 2026
spydon added a commit to supabase/supabase-flutter that referenced this pull request Oct 30, 2026
…he SDK spec (#2114)

Aligns the Realtime client with the connection recovery specs in
supabase/sdk#304, including the fixed heartbeat
beat from
supabase/sdk#304 (comment).

## Connect (`realtime.client.connect`)

- The default reconnect delay is now full jitter with a 1 second base
and a 30 second cap: a random duration between zero and `min(30 s, 1 s ×
2^(attempt − 1))`. It used to be a delay doubling from 1 to 10 seconds
without jitter.
- The reconnect attempt count resets when a socket opens, instead of
after the access token resolved. A connection that opens and then fails
its token resolution therefore reconnects as attempt 1.

## Subscribe (`realtime.channel.subscribe`)

- Channel rejoins use their own fixed delays of 1, 2, 5 and then 10
seconds, instead of `reconnectAfter`.
- A join rejected with `ChannelRateLimitReached`,
`ConnectionRateLimitReached`, `ClientJoinRateLimitReached` or
`DatabaseConnectionRateLimitReached` waits 30 seconds before the next
rejoin.
- A join answered with an error is now retried like a join that timed
out. It used to leave the channel errored until the next reconnect.
- Rejoins are only scheduled while the socket is open. When a socket
opens, every channel's rejoin attempt count resets and errored channels
rejoin at once.

## Heartbeat (`realtime.configuration.heartbeat_interval`)

- The heartbeat already runs at a fixed beat, as the spec now requires.
- A missing reply now treats the socket as dead at once: channels are
marked as errored, the socket subscription is dropped so no further
messages are dispatched, a reconnect is scheduled, and the sink is
closed. The client used to close the sink and wait for it to close, up
to `connectionCloseTimeout`, while still dispatching messages.

## Reconnect backoff (`realtime.configuration.reconnect_backoff`)

- `reconnectAfter` now only sets the socket reconnect delay. A negative
delay already counts as zero, since `Timer` treats it that way.

`MIGRATION.md` gets a section on the new defaults and on
`reconnectAfter` no longer driving rejoins. New tests in
`connection_recovery_test.dart` cover the rejoin steps, the rate limit
wait, the reset on open, the missing heartbeat reply and the fixed beat.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Realtime reconnect delays are randomized and can increase up to 30
seconds.
* Channel rejoin delays follow a separate schedule: 1, 2, 5, then 10
seconds. Rate-limited joins wait 30 seconds.
* Failed channel joins retry when the socket is connected, and channels
rejoin after reconnection.
* An unanswered heartbeat now closes the socket promptly so reconnection
can begin.

* **Documentation**
* Updated the migration guide to explain reconnect and rejoin delays,
rate-limit handling, and heartbeat behavior.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
spydon added a commit to supabase/supabase-py that referenced this pull request Nov 3, 2026
**What this changes**

Channel rejoin now follows the Realtime `channel/subscribe` spec
proposed in supabase/sdk#304.

**The problem**

The rejoin timer computed its delay as `2**(tries + 1)`, so the first
rejoin waited 4 seconds and later ones grew without limit. On top of
that:

- The rejoin timer callback rescheduled itself every time it fired, so a
channel kept resending its join on the backoff interval, even while a
join was still waiting for a reply.
- A join that the server rejected with an error reply never scheduled a
rejoin.
- Rate-limit join errors were retried on the normal schedule.
- After a reconnect, only channels that were joined or joining got
rejoined. Channels that were already errored waited for their timer, and
attempt counts were never reset.

**The fix**

- Rejoin delays are 1, 2, 5 and 10 seconds for attempts 1 to 4, then 10
seconds for every later attempt.
- `AsyncTimer` passes the actual attempt number to its delay function,
and `schedule_timeout` accepts an explicit delay override.
- A join that fails or times out schedules a rejoin, but only while the
socket is open. The rejoin timer no longer reschedules itself.
- A join rejected with `ChannelRateLimitReached`,
`ConnectionRateLimitReached`, `ClientJoinRateLimitReached` or
`DatabaseConnectionRateLimitReached` waits 30 seconds before the next
rejoin.
- When the socket opens, every channel's rejoin attempt count resets,
and each errored channel rejoins at once.
- `_reconnect` remembers which channels were joined or joining before
the socket dropped, and those channels rejoin on the new socket even if
a late join reply from the old connection marks them joined during the
handshake. Each channel rejoins once, and channels that were removed or
are being left are skipped.

The socket reconnect curve (`AsyncRealtimeClient.connect`) is unchanged
here.

**Behavior change**

A join rejected for a reason other than rate limiting (for example
`Unauthorized`) is now retried on the stepped schedule, matching the
spec and phoenix.js. Before, the channel stayed errored after a join
error reply.

**Test**

`tests/test_channel_rejoin.py` covers the stepped schedule, join errors,
the rate-limit delay, no rejoin while disconnected, no
self-rescheduling, the reset and immediate rejoin on socket open, and
rejoining a channel that a stale reply marked joined during a reconnect.
Verified with ruff, mypy, and the full realtime suite against local
infra.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved channel recovery after connection interruptions. Channels
retry joining only while connected, avoiding repeated retries during
disconnection.
* Adjusted retry timing for join timeouts and errors, including a
30-second delay for rate-limited errors and retry intervals capped at 10
seconds.
* After reconnecting, eligible channels are rejoined and their retry
timers are reset, helping restore channel availability.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Lukas Klingsbo <lukas.klingsbo@gmail.com>

@spydon spydon left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@grdsdev
grdsdev added this pull request to stack #349 November 10, 2026 19:51
@grdsdev
grdsdev merged commit baed7a7 into main Nov 10, 2026
8 of 9 checks passed
@grdsdev
grdsdev deleted the claude/realtime-configuration-specs-1bc7bc branch November 10, 2026 19:51
grdsdev added a commit that referenced this pull request Nov 10, 2026
Stacked on #304. Merge #304 first; this PR then retargets to `main`.

Adds a "Server channel error" section to
`specs/realtime/channel/subscribe.md`:

- A `phx_error` for a stale `join_ref` changes nothing.
- On a joined channel, the client reports the error, marks the channel
errored, and rejoins with the rejoin delays (1/2/5/10 s, then 10 s). A
push that waits for a reply fails when its own timeout ends.
- On a join in flight, the `phx_error` counts as a failed join attempt.
Subscribe keeps waiting.
- A confirmed rejoin reports the channel as subscribed again.
- A socket close during the delay cancels it. The channel rejoins when
the socket opens.
- `phx_close` removes the channel and does not rejoin.

The rules follow `@supabase/phoenix` 0.4.5 `channel.js`. The state
diagram gains the `phx_error` and `phx_close` transitions.
`presence_state.md` rule 5 assumes this rejoin, and the new section
links it. That file is on `main`, so the link resolves once #304 merges.

Context: SDK-2609 (supabase-swift v3 does not rejoin after a server
`phx_error`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants