Skip to content

perf: coalesce presence polling across tabs of the same user - #300

Merged
josephfusco merged 9 commits into
WordPress:mainfrom
i-am-chitti:feat/269-coalesce-polling
Aug 19, 2026
Merged

josephfusco merged 9 commits into
WordPress:mainfrom
i-am-chitti:feat/269-coalesce-polling

Conversation

@i-am-chitti

Copy link
Copy Markdown
Collaborator

Fixes #269

Summary

When the same user has an admin screen open in multiple tabs, each tab independently pings Heartbeat, triggering its own wp_set_presence() write and Who's Online/Active Posts/stale-screen lookup - redundant work that scales with tab count, not with actual viewers.

Elects one tab per context (Web Locks) to send the ping; sibling tabs skip their own ping and instead receive the elected tab's response over BroadcastChannel, so their widgets/banners stay live. Closing the elected tab promotes a sibling automatically. Tabs on a different post or screen are never coalesced with each other.

Covers all three tab-scoped ping mechanisms in the plugin:

  • presence-ping / presence-editor-ping (admin + editor rooms, Who's Online) - dedupe key is screen + post + front-page context
  • presence-screen-ping (stale-screen banner) - dedupe key is screenKey
  • presence-active-posts-ping (Active Posts widget) - fixed dedupe key, since this ping carries no per-page context (sitewide data). Required first extracting its script from inline PHP into an external file (assets/js/active-posts.js) so it could be enqueued and coalesced like the other two.

Also ships usePresenceUsers, a React hook + coordinator for third-party consumers of the public presence REST endpoint - a related but separate improvement, unaffected by the wp-admin fix above since nothing in this plugin's own UI uses that hook yet.

Test plan

  • New unit tests for the coordinator (presence-poll-coordinator.test.js)
  • New e2e tests per mechanism: election, promotion on leader-tab close, response relay, different-post/screen isolation
  • npm run test - no regressions
  • npm run test:e2e - no regressions
  • Manual multi-tab testing across all three mechanisms
Screenshots image image image image image
Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5
Used for: Assisting with implementation

@github-actions

Copy link
Copy Markdown
Contributor

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props iamchitti, joefusco.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Aug 19, 2026 •

Copy link
Copy Markdown
Contributor

▶ Preview in WordPress Playground

Open in WordPress Playground

Boots a fresh WordPress with this PR's presence-api build, seeds 5 demo users, and drops you on the dashboard.

Stress-test variant: 40 demo users · Built from 0911c62c1fc51d0256b3446e55ce96e621117764. Auto-updates when you push.

@codecov

codecov Bot commented Aug 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.55556% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.57%. Comparing base (7f43eeb) to head (0911c62).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #300      +/-   ##
============================================
+ Coverage     78.41%   79.57%   +1.16%     
  Complexity      190      190              
============================================
  Files            14       14              
  Lines          2052     2071      +19     
============================================
+ Hits           1609     1648      +39     
+ Misses          443      423      -20     
Flag Coverage Δ
multisite 79.57% <80.55%> (+1.16%) ⬆️
phpunit 78.41% <80.55%> (+1.17%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@i-am-chitti i-am-chitti added [Type] Enhancement A suggestion for improvement of an existing feature [Area] Heartbeat 💓 Issues for the heartbeat subsystem [Area] Widgets Issues for the dashboard widgets Performance Work relates to query load, cache behavior, or scaling php Pull requests that update php code javascript Pull requests that update javascript code labels Aug 19, 2026
@josephfusco

Copy link
Copy Markdown
Collaborator

The only concern from review was that the tab-election/relay logic was duplicated across presence-ping.js, active-posts.js, and stale-screen.js, so I pushed a commit extracting it into a shared tab-coordinator.js helper, verified with the existing coalescing e2e tests plus the full PHPUnit/PHPCS/PHPStan suite.

@josephfusco josephfusco left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for knocking this out, nice work with this!

@josephfusco
josephfusco merged commit 8697918 into WordPress:main Aug 19, 2026
17 of 18 checks passed
@i-am-chitti
i-am-chitti deleted the feat/269-coalesce-polling branch August 19, 2026 18:10
@josephfusco josephfusco mentioned this pull request Sep 24, 2026
24 of 36 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Area] Heartbeat 💓 Issues for the heartbeat subsystem [Area] Widgets Issues for the dashboard widgets javascript Pull requests that update javascript code Performance Work relates to query load, cache behavior, or scaling php Pull requests that update php code [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Coalesce polling across tabs of the same user

2 participants