Repository navigation
Description
Provide environment information
System:
OS: macOS 15.7
CPU: (10) arm64 Apple M1 Max
Memory: 113.00 MB / 64.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.18.1 - ~/n/bin/node
Yarn: 4.9.2 - ~/n/bin/yarn
npm: 10.8.2 - ~/n/bin/npm
bun: 1.2.22 - /opt/homebrew/bin/bun
Browsers:
Edge: 140.0.3485.81
Safari: 26.0.1
npmPackages:
@tanstack/react-query: 5.90.1 => 5.90.1
@trpc/client: 11.5.1 => 11.5.1
@trpc/server: 11.5.1 => 11.5.1
@trpc/tanstack-react-query: 11.5.1 => 11.5.1
react: 18.3.1 => 18.3.1
typescript: 5.7.3 => 5.7.3
Describe the bug
When a subscription is closed on the server side when using @trpc/tanstack-react-query and using the wsLink and WebsocketClient from createWSClient(), I'm not seeing the subscription state change from pending => idle. The subscription always stays in the pending state. The websocket is still connected, it's just the subscription state itself that is inaccurate.
Most of our subscriptions have idle timeouts configured so that the server will close the connection after some period of inactivity. This works fine when using SSE or when using SSE or Websockets with @trpc/react-query, but when I tried migrating to @trpc/tanstack-react-query, we're seeing this behaviour. This is problematic for our application because we have a separate mutation that is called to kick off some work, the results of which are broadcast to the client over the subscription. We check the state of the subscription before submitting work, and if it's idle, we reconnect by calling reset() on the subscription. Since the status isn't updating, the subscription eventually stops and then the client is stuck never receiving events until they refresh.
Link to reproduction
To reproduce
- Ensure that you're looking at the vite server by confirming the port "3000" in the url of the preview browser (https://githubebaymo2t-4gxi--3000--96435430.local-corp.webcontainer.io)
- Watch the logs from the backend server in the terminal. It should say "listening for events" when started.
- Click "Start" immediately.
- You should see events streaming from the backend to the browser
- Redo steps 3-4 a couple of times to ensure that you can regularly start the "worker" on the backend and get events.
- 5 seconds after the last event is recieved you should see "going idle" in the terminal. This will indicate that the server has closed the subscription using
return - Notice that the status in the browser still says "pending".
- If you click "start work" in the browser, you can see the events being generated by the server, but no events are making it to the browser, and the subscription status is still "pending".
- Refresh the browser
- Click "Start" immediately
- See the events streaming normally as before.
Since we rely on the subscription status to determine when to restart the subscription, the client gets broken when using websockets, @trpc/tanstack-react-query and subscriptions.
Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR fixing this bug!
Activity
KATT commentedon Sep 29, 2025
The
connectionStateis actually about the underlying connection & not about this specific subscription, so it's rather that we need to clarify this in docsKATT commentedon Sep 29, 2025
onCompletewill be called when the subscription ends on the serverKATT commentedon Sep 29, 2025
Or that was how it was intended, but not how we implemented it... 👀
KATT commentedon Sep 30, 2025
There is a bug here, but the bug is that the connection state updates from "pending" -> "connecting" when starting the subscription 🙃
noseworthy commentedon Oct 1, 2025
Oh! So, this code works when using@trpc/react-querywith the websocket. And by works, I mean that the connection state is the state of the subscription, not the underlying websocket. So I can see the idle -> connecting -> pending -> idle transitions even though the underlying websocket stays open.So this is an intentional breaking change between the twouseSubscription()calls?Edit: Actually, maybe I misunderstood. Sure, the
onConnectionStateChange()is only useful for the underlying websocket connection state. But thesubscription.statusreturned byuseSubscription()is supposed to be whether the subscription isidle,connecting,pending, orerrorright? Also, I don't see any way to include anonCompleted()handler tosubscriptionOptions()?fix(client): do not emit "connecting" when initializing websocket sub…
Merge pull request #1 from rmngqibisa/fix/issue-6962-ws-subscription-…
fix(tanstack-react-query): reset useSubscription to idle when the ser…
github-actions commentedon Nov 4, 2026
This issue has been locked because we are very unlikely to see comments on closed issues. If you are running into a similar issue, please create a new issue. Thank you.