Skip to content

fix(realtime): preserve presence refs - #2566

Merged
mandarini merged 3 commits into
supabase:masterfrom
7ttp:fix/realtime-presence-meta-mutation
Jul 27, 2026
Merged

mandarini merged 3 commits into
supabase:masterfrom
7ttp:fix/realtime-presence-meta-mutation

Conversation

@7ttp

@7ttp 7ttp commented Jul 27, 2026

Copy link
Copy Markdown
Member

TL;DR

Build presence event payloads from copies so live presence refs remain intact and
departed members are removed correctly.

ref:

@7ttp
7ttp requested review from a team as code owners July 27, 2026 13:15
@coderabbitai

coderabbitai Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 61d258c8-56f4-4112-94d4-06728183b634

📥 Commits

Reviewing files that changed from the base of the PR and between d4652f3 and 01d11fb.

📒 Files selected for processing (1)
  • packages/core/realtime-js/test/phoenix/presenceAdapter.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/realtime-js/test/phoenix/presenceAdapter.test.ts

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Updated presence transformation to stop mutating the original presence objects.
    • Improved join/leave diff handling so updates don’t duplicate members and presence keys are removed once all related metadata has left.
    • Preserved __proto__ handling to prevent prototype-pollution risks.
  • Tests

    • Added coverage for presence diff payload behavior through join/leave processing.
    • Expanded regression checks for correct state cleanup across sequential join/leave operations.

Walkthrough

transformState now copies each presence metadata object, preserving property descriptors, before renaming phx_ref to presence_ref and removing Phoenix-specific fields. Tests verify that __proto__ remains an own data property and that sequential join/leave diffs remove a presence key after all metadata entries leave.

Assessment against linked issues

Objective Addressed Explanation
Prevent shared presence metadata mutation and remove departed presence keys [#2565] ✅

Possibly related PRs

Suggested labels: realtime-js

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: lockfile failed supply-chain policy check. Run pnpm install locally to update the lockfile.


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

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Jul 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2566

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2566

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2566

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2566

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2566

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2566

commit: 01d11fb

@coveralls

coveralls commented Jul 27, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 80.649% (-0.006%) from 80.655% — 7ttp:fix/realtime-presence-meta-mutation into supabase:master

@mandarini mandarini self-assigned this Jul 27, 2026

@mandarini mandarini 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.

One small thing before I merge. Can we please add a test for the presence update path? Your test covers two devices under one key. The more common trigger is a single client calling track() again with a changed payload: the server responds with Phoenix.Presence.update (presence_handler.ex L162-163), which sends a join and a leave for the same key in a single diff. That shape duplicates the member immediately with the stale payload still visible, and then both metas ghost on disconnect. It is a different symptom from the one you assert on, so it needs its own case:

test('collapses a presence update instead of duplicating the member', () => {
  const key = 'user-123'
  let state: PresenceStates = { [key]: { metas: [{ phx_ref: 'r1', status: 'online' }] } }

  // track() with a changed payload: the server sends Presence.update as join + leave
  state = Presence.syncDiff(
    state,
    {
      joins: { [key]: { metas: [{ phx_ref: 'r2', phx_ref_prev: 'r1', status: 'away' }] } },
      leaves: { [key]: { metas: [{ phx_ref: 'r1', status: 'online' }] } },
    },
    PresenceAdapter.onJoinPayload,
    PresenceAdapter.onLeavePayload
  )
  expect(state[key].metas).toHaveLength(1)

  state = Presence.syncDiff(
    state,
    { joins: {}, leaves: { [key]: { metas: [{ phx_ref: 'r2', status: 'away' }] } } },
    PresenceAdapter.onJoinPayload,
    PresenceAdapter.onLeavePayload
  )
  expect(state).not.toHaveProperty(key)
})

toHaveLength(1) is 2 without your change and 1 with it, so it earns its place.

@mandarini
mandarini merged commit 31dc1b0 into supabase:master Jul 27, 2026
23 checks passed
@7ttp
7ttp deleted the fix/realtime-presence-meta-mutation branch July 27, 2026 14:57
mandarini pushed a commit to supabase/supabase that referenced this pull request Jul 27, 2026
This PR updates @supabase/*-js libraries to version 2.110.9.

**Source**: supabase-js-stable-release

**Changes**:
- Updated @supabase/supabase-js to 2.110.9
- Updated @supabase/auth-js to 2.110.9
- Updated @supabase/realtime-js to 2.110.9
- Updated @supabase/postgest-js to 2.110.9
- Refreshed pnpm-lock.yaml

---

## Release Notes

## v2.110.9

## 2.110.9 (2026-07-27)

### 🩹 Fixes

- **auth:** downgrade stale refresh token console noise
([#2559](supabase/supabase-js#2559))
- **realtime:** preserve presence refs
([#2566](supabase/supabase-js#2566))
- **repo:** override sharp to >=0.35.0 to clear libvips advisory
([#2548](supabase/supabase-js#2548))
- **repo:** populate symbols in sdk-compliance so capabilities are
verifiable ([#2547](supabase/supabase-js#2547))
- **repo:** bump postcss, babel, next to clear audit advisories
([#2561](supabase/supabase-js#2561))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini
- Vaibhav @7ttp

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
mandarini pushed a commit to supabase/ssr that referenced this pull request Jul 27, 2026
This PR updates `@supabase/supabase-js` to v2.110.9.

**Source**: supabase-js-stable-release

---

## Release Notes

## v2.110.9

## 2.110.9 (2026-07-27)

### 🩹 Fixes

- **auth:** downgrade stale refresh token console noise
([#2559](supabase/supabase-js#2559))
- **realtime:** preserve presence refs
([#2566](supabase/supabase-js#2566))
- **repo:** override sharp to >=0.35.0 to clear libvips advisory
([#2548](supabase/supabase-js#2548))
- **repo:** populate symbols in sdk-compliance so capabilities are
verifiable ([#2547](supabase/supabase-js#2547))
- **repo:** bump postcss, babel, next to clear audit advisories
([#2561](supabase/supabase-js#2561))

### ❤️ Thank You

- Katerina Skroumpelou @mandarini
- Vaibhav @7ttp

This PR was created automatically.

Co-authored-by: supabase-workflow-trigger[bot] <266661614+supabase-workflow-trigger[bot]@users.noreply.github.com>
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.

Presence: payload building mutates shared metas (deletes phx_ref), leaves never match, departed members stay online forever

3 participants