Skip to content

chore(PLA-2218): enable socket disconnection when page visibility switches to hidden#866

Merged
jareddellitt merged 7 commits into
mainfrom
jdellitt/PLA-2218-V2
Feb 18, 2026
Merged

chore(PLA-2218): enable socket disconnection when page visibility switches to hidden#866
jareddellitt merged 7 commits into
mainfrom
jdellitt/PLA-2218-V2

Conversation

@jareddellitt
Copy link
Copy Markdown
Contributor

Description

Centralizes page visibility handling to a PageVisibility class and adds it directly to the ApiClient. Previously this was handled directly by feeds (and only for feeds), and was disabled by default. This enables it by default, manages for all socket usages, and sets a 30s timeout which gives a little bit of flexibility for users switching back and forth between tabs.

Checklist

  • Tests have been added for new features or major refactors to existing features.

@linear
Copy link
Copy Markdown

linear Bot commented Feb 17, 2026

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Feb 17, 2026

🦋 Changeset detected

Latest commit: 47eb095

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@knocklabs/client Patch
client-example Patch
guide-example Patch
@knocklabs/expo Patch
@knocklabs/react-core Patch
@knocklabs/react-native Patch
@knocklabs/react Patch
@knocklabs/expo-example Patch
ms-teams-connect-example Patch
nextjs-app-dir-example Patch
nextjs-example Patch
slack-connect-example Patch
slack-kit-example Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link
Copy Markdown

vercel Bot commented Feb 17, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
javascript-ms-teams-connect-example Ready Ready Preview, Comment Feb 18, 2026 11:14pm
javascript-nextjs-example Ready Ready Preview, Comment Feb 18, 2026 11:14pm
javascript-slack-connect-example Ready Ready Preview, Comment Feb 18, 2026 11:14pm
javascript-slack-kit-example Ready Ready Preview, Comment Feb 18, 2026 11:14pm

Request Review

Copy link
Copy Markdown

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Comment thread packages/client/test/pageVisibility.test.ts
@jareddellitt jareddellitt marked this pull request as ready for review February 18, 2026 16:19
@jareddellitt jareddellitt requested review from a team, mattmikolay and thomaswhyyou and removed request for a team February 18, 2026 16:51
},
});

if (options.disconnectOnPageHidden !== false) {
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.

I think turning this on by default will have downstream implications for other modules/clients that rely on the socket connection provided by this API client? For example, I know at least the guide client uses the socket connection to listen for real time updates from the guides channel API.

I'm guessing the right thing to do there is to re-fetch guides when the socket re-connects, not sure if we need to necessarily re-subscribe.

Not saying we need to do all of that in this PR, but just wanted to surface this so we can think about how we go about handling downstream implications.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I assumed (maybe incorrectly) that this would already be handled if a socket were to become disconnected (like a deploy). Looks like regardless of this change we'd want to add some sort of recovery logic in case of a disconnection?

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.

if a socket were to become disconnected (like a deploy)

In the case of a deploy that leads to a socket disconnection, can I double check my understanding that the api client automatically tries and reconnects with the new deploy? i.e. if we roll switchboard-sockets, then all the clients with a socket connection ends up disconnecting for a bit (~ minutes) until it reconnects? If that is right, then I don't think it's super critical to do anything more at least with the guide client.

The scenario that i'm thinking about is, where a user has a tab open but in "hidden" state for an extend period of time (~ days) and comes back to it, then we probably want to let the guide client re-fetch to "reset".

So to answer your q: I haven't thought too hard about handling socket disconnection scenarios up until now, assuming the main scenario is a new deploy in which case it auto reconnects. But turning the option on by default to auto disconnect on page visibility does make me think we need to think about handling socket connection scenarios more. Again, don't think that is a blocker to ship. cc: @cjbell

Comment thread .changeset/tasty-zoos-appear.md Outdated
"@knocklabs/client": patch
---

Disconnects socket after an initial delay when page visibility is hidden
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.

We may want to consider mentioning in this changeset that the auto_manage_socket_connection and auto_manage_socket_connection_delay options have been removed.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 18, 2026

Codecov Report

❌ Patch coverage is 96.87500% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.41%. Comparing base (cb7148b) to head (47eb095).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
packages/client/src/api.ts 81.81% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #866      +/-   ##
==========================================
+ Coverage   67.30%   67.41%   +0.10%     
==========================================
  Files         203      204       +1     
  Lines        8553     8561       +8     
  Branches     1112     1118       +6     
==========================================
+ Hits         5757     5771      +14     
+ Misses       2772     2766       -6     
  Partials       24       24              
Files with missing lines Coverage Δ
packages/client/src/clients/feed/feed.ts 81.50% <ø> (-0.32%) ⬇️
packages/client/src/knock.ts 100.00% <100.00%> (ø)
packages/client/src/pageVisibility.ts 100.00% <100.00%> (ø)
packages/client/src/api.ts 98.07% <81.81%> (-1.93%) ⬇️

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