Repository navigation
fix(wsLink): wsLink should await for connectionParams before sending β¦ - #6547
Conversation
|
This PR was not deployed automatically as @hmatthieu does not have access to the Railway project. In order to get automatic PR deploys, please add @hmatthieu to the project inside the project settings page. |
|
The latest updates on your projects. Learn more about Vercel for Git βοΈ
1 Skipped Deployment
|
@trpc/client
@trpc/next
@trpc/server
@trpc/react-query
@trpc/tanstack-react-query
@trpc/upgrade
commit: |
WalkthroughThis update simplifies and refines the WebSocket connection process. In the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
π Recent review detailsConfiguration used: CodeRabbit UI π Files selected for processing (1)
π§ Files skipped from review as they are similar to previous changes (1)
β° Context from checks skipped due to timeout of 90000ms (20)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? πͺ§ TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 1
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (3)
packages/client/src/links/wsLink/wsClient/wsClient.ts(1 hunks)packages/client/src/links/wsLink/wsClient/wsConnection.ts(2 hunks)packages/tests/server/websockets.test.ts(2 hunks)
π Additional comments (2)
packages/client/src/links/wsLink/wsClient/wsClient.ts (1)
23-24: LGTM! Simplified imports.The removal of
buildConnectionMessagefrom imports is correct as the connection message handling has been moved towsConnection.ts.packages/tests/server/websockets.test.ts (1)
1766-1786: LGTM! Comprehensive test for async auth.The test case effectively validates that:
- The WebSocket client waits for async connectionParams to resolve
- The connection is established with the correct auth token
- The whoami endpoint returns the expected user data
|
@hmatthieu this may have introduced a regression, ping timeout/reconnect seems to have stopped working with this PR. |
@TomKaltz While the connectionParams async function resolves, reconnection will indeed not be active. But IMO that's fine β we don't want to retry if we haven't even finished our previous attempt, right? Or are you talking about something else ? |
Yes, upon a loss in network the ping never times out to close the current connection. When the network comes back up a new connection is forced but connectionParams are not sent on the new connection, resulting in authentication errors. |
|
My other concern is that the connection is not being forcefully closed upon ping timeout like before. Therefore user code is not notified of the loss in connection until the network comes back which is unintuitive. |
|
@TomKaltz After further investigation: Preliminary notes: Previous wsLink behavior regarding keep-alive: After refactoring: I couldn't reproduce your latest concern. I'll push my latest commit to this PR and would appreciate your feedback. |
β¦ ready (after connection message is sent)
There was a problem hiding this comment.
Actionable comments posted: 0
π§Ή Nitpick comments (1)
packages/tests/server/websockets.test.ts (1)
1696-1698: Consider improving the never-resolving Promise implementation.The empty arrow function in the Promise constructor could be made more explicit about its intended behavior.
- iterable: t.procedure.subscription(async function* () { - await new Promise(() => {}); - }) + iterable: t.procedure.subscription(async function* () { + await new Promise((resolve) => { + // Intentionally never resolve to keep subscription active + }); + })π§° Tools
πͺ ESLint
[error] 1697-1697: Unexpected empty arrow function.
(@typescript-eslint/no-empty-function)
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (3)
packages/client/src/links/wsLink/wsClient/wsClient.ts(2 hunks)packages/client/src/links/wsLink/wsClient/wsConnection.ts(3 hunks)packages/tests/server/websockets.test.ts(3 hunks)
π§ Files skipped from review as they are similar to previous changes (2)
- packages/client/src/links/wsLink/wsClient/wsClient.ts
- packages/client/src/links/wsLink/wsClient/wsConnection.ts
π§° Additional context used
πͺ ESLint
packages/tests/server/websockets.test.ts
[error] 1697-1697: Unexpected empty arrow function.
(@typescript-eslint/no-empty-function)
β° Context from checks skipped due to timeout of 90000ms (20)
- GitHub Check: E2E-tests (fastify-server)
- GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
- GitHub Check: E2E-tests (express-server)
- GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
- GitHub Check: E2E-tests (express-minimal)
- GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 20.x)
- GitHub Check: Release using pkg.pr.new
- GitHub Check: E2E-tests (cloudflare-workers)
- GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
- GitHub Check: E2E-tests (.test/ssg)
- GitHub Check: Test a monorepo using built declaration files
- GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
- GitHub Check: E2E-tests (.test/internal-types-export)
- GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
- GitHub Check: E2E-tests (.test/diagnostics-big-router)
- GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
- GitHub Check: E2E-tests (.experimental/next-app-dir)
- GitHub Check: E2E-tests (Deno) (deno-deploy)
- GitHub Check: test
- GitHub Check: Lint and auto-fix
π Additional comments (2)
packages/tests/server/websockets.test.ts (2)
1769-1789: LGTM! Well-structured async auth test.The test effectively validates that the WebSocket client waits for async connection parameters before making requests. Good use of sleep to simulate real-world delays in token retrieval.
1791-1827: LGTM! Comprehensive reconnection test with async auth.The test thoroughly validates that:
- Initial connection and subscription work with async auth
- Reconnection properly handles pending subscriptions
- Connection events are triggered in the correct order
Good use of mock functions to track the sequence of events.
There was a problem hiding this comment.
Actionable comments posted: 1
π Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
π Files selected for processing (1)
packages/tests/server/websockets.test.ts(3 hunks)
π§° Additional context used
πͺ Biome (1.9.4)
packages/tests/server/websockets.test.ts
[error] 1696-1698: This generator function doesn't contain yield.
(lint/correctness/useYield)
β° Context from checks skipped due to timeout of 90000ms (19)
- GitHub Check: E2E-tests (fastify-server)
- GitHub Check: E2E-tests (express-server)
- GitHub Check: E2E-tests (express-minimal)
- GitHub Check: E2E-tests (cloudflare-workers)
- GitHub Check: E2E-tests (.test/ssg)
- GitHub Check: E2E-tests (.test/internal-types-export)
- GitHub Check: e2e-legacy-node (next-prisma-todomvc, 20.x)
- GitHub Check: Release using pkg.pr.new
- GitHub Check: e2e-legacy-node (next-prisma-todomvc, 18.x)
- GitHub Check: Test a monorepo using built declaration files
- GitHub Check: E2E-tests (.test/diagnostics-big-router)
- GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 20.x)
- GitHub Check: E2E-tests (.experimental/next-app-dir)
- GitHub Check: e2e-legacy-node (next-prisma-websockets-starter, 18.x)
- GitHub Check: E2E-tests (Bun) (bun, ubuntu-latest)
- GitHub Check: e2e-legacy-node (next-prisma-starter, 20.x)
- GitHub Check: e2e-legacy-node (next-prisma-starter, 18.x)
- GitHub Check: E2E-tests (Deno) (deno-deploy)
- GitHub Check: test
π Additional comments (3)
packages/tests/server/websockets.test.ts (3)
1769-1789: LGTM! Well-structured test for async authentication.The test effectively validates that the WebSocket client properly awaits async connection parameters before proceeding with queries.
1791-1827: LGTM! Comprehensive test for reconnection with async auth.The test thoroughly verifies that:
- The connection opens correctly with async auth
- Subscriptions start properly
- Reconnection works without duplicating subscriptions
- The WebSocket client maintains the expected state
16-19:Details
β Verification successful
Verify the use of unstable core imports.
The code imports from
@trpc/server/unstable-core-do-not-import. This path suggests the module is not intended for production use. Since this is a test file, it's acceptable, but we should verify if there's a stable alternative available.
π Web query:
Is there a stable alternative to @trpc/server/unstable-core-do-not-import in tRPC?π‘ Result:
tRPC has been actively working to reduce reliance on
@trpc/server/unstable-core-do-not-importby migrating functionality to stable entry points[1][9]. While there isn't a direct 1:1 replacement for all unstable exports yet, here are the current alternatives:Stable Alternatives:
- Use standard imports from
@trpc/serverinstead of unstable paths[1][3]- Leverage official packages like:
@trpc/client@trpc/react-query@trpc/next[1]- For plugins, use the new
@trpc/server/pluginentrypoint (recently added)[1][9]Key Recommendations:
- Avoid direct imports from
unstable-core-do-not-importunless absolutely necessary[3][8]- Ensure all
@trpc/*packages share matching versions[3]- Use TypeScript β₯5.6.2 with strict mode enabled[3]
- Monitor official releases for newly stabilized APIs[9]
The tRPC team maintains strict semver compliance for non-unstable exports and considers type changes as breaking changes[3]. Features marked
unstable_remain production-safe but may see minor API adjustments, whileexperimental_features carry higher instability risks[3].For developers encountering type errors when upgrading (like ReadableStream issues), adding
"DOM"totsconfig.json's lib array often resolves them[5]. When migrating large codebases, consider automated API change detection through OpenAPI specifications[7].Citations:
- 1: https://polar.sh/trpc/trpc/issues/5484
- 2: https://discord-questions.trpc.io/u/943602057565327420
- 3: https://trpc.io/faq
- 4: https://discord-questions.trpc.io/m/1257597752976605261
- 5: https://discord-questions.trpc.io/m/1280593106311450665
- 6: https://tanstack.com/query/latest/docs/framework/react/guides/migrating-to-v5
- 7: https://blog.stackademic.com/harnessing-openapi-to-track-and-domesticate-wild-trpc-changes-050b24b33a76
- 8: https://github.com/trpc/trpc/blob/next/packages/server/src/unstable-core-do-not-import/rootConfig.ts
- 9: #5484
- 10: https://www.tdx.cat/bitstream/10803/57466/2/belda.pdf.txt
Usage of Unstable Core Imports Confirmed
The import from
@trpc/server/unstable-core-do-not-importinpackages/tests/server/websockets.test.tsis acceptable in this test context. The tRPC documentation confirms that while a complete 1:1 stable alternative for all unstable exports isnβt yet available, stable imports (e.g., from@trpc/server) are being progressively introduced. For now, using these unstable exports in tests is fine, but please consider migrating to the stable alternatives as they become fully supported in future releases.
|
I can confirm that proper reconnect upon network restore is now working. What is not working optimally IMHO is that the original connection is not forcefully closed when a keepaline ping times out. Due to this, the connection just hangs open until the network connection returns, then the original connection is finally closed and a new one is opened. I'm using chrome devtools network tab "offline" throttling to simulate a dropped connection. |
|
This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you. |

π― Changes
Added a test case to ensure wsLink awaits urlConnectionParams before sending any messages. urlConnectionParams resolution is now handled at the wsConnection level. The wsClient can use the WebSocket once the connection parameters are sent.
β Checklist
Summary by CodeRabbit
Refactor
Tests