You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat: access subscription connection status from tRPC client #5871
My app uses tRPC's subscriptions via websockets. I would like a way to display an indicator if the client was disconnected from the websocket (ex. went offline).
Trying to use onOpen and onClose in createWSClient() for this kinda works, but it seemed like gracefully disconnecting from the subscriptions was triggering onClose, and I couldn't figure out how to distinguish from the disconnect code whether it was due to an error or a graceful disconnect.
Describe the solution you'd like to see
Some way to access a general status of subscriptions from the tRPC client (ex. via React context).
Maybe calling .useSubscription() could return an object with the status of that specific subscription.
Describe alternate solutions
N/A
Additional information
No response
👨👧👦 Contributing
🙋♂️ Yes, I'd be down to file a PR implementing this feature!
Currently working on a bit of a proof of concept making a few assumptions on how the observables and subscriptionLinks work. Currently I am running into the issue that there is no event that SSE subscriptions emit to tell if they are disconnected, the only thing you can do is have an interval monitor the readyState (assumption), which brings it's own set of problems.
We should probably allow to programmatically retry the hook in case of non-recoverable errors too. And for it to remember the last emitted sse()-id then too, I added support for tracking ids in WebSockets in #5868
The problem is that browsers handle reconnects automatically and there is no event that gets emitted when the EventSource loses connection. The only thing you can do is periodically check the readyState.
Good catch, it does actually emit an error event when the connection is interrupted. I added a connecting result to the envelope, and am now listening to error's, when the readyState is CONNECTING I pass a connecting result to the observer. Also passing a connecting result on startup.
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.
Activity
KATT commentedon Jul 5, 2024
useSubscription's return should return some of this info indeedBeBoRE commentedon Jul 9, 2024
Currently working on a bit of a proof of concept making a few assumptions on how the observables and subscriptionLinks work. Currently I am running into the issue that there is no event that SSE subscriptions emit to tell if they are disconnected, the only thing you can do is have an interval monitor the readyState (assumption), which brings it's own set of problems.
KATT commentedon Jul 9, 2024
Yeah we need to add more event types that can be passed down the chain, similar to the started event one
The code around the observable and the events passed down haven't been touched for a really long time, I'd have to refresh my memory a bit 😅
KATT commentedon Jul 9, 2024
We should probably allow to programmatically retry the hook in case of non-recoverable errors too. And for it to remember the last emitted
sse()-id then too, I added support for tracking ids in WebSockets in #5868BeBoRE commentedon Jul 9, 2024
The problem is that browsers handle reconnects automatically and there is no event that gets emitted when the EventSource loses connection. The only thing you can do is periodically check the readyState.
KATT commentedon Jul 9, 2024
Not entirely correct - I think we get error events that we just ignore when it leads to a reconnect
trpc/packages/server/src/unstable-core-do-not-import/stream/sse.ts
Lines 245 to 248 in 8f18c40
BeBoRE commentedon Jul 9, 2024
Good catch, it does actually emit an error event when the connection is interrupted. I added a connecting result to the envelope, and am now listening to error's, when the readyState is CONNECTING I pass a connecting result to the observer. Also passing a connecting result on startup.
BeBoRE commentedon Jul 9, 2024
I'll create a draft PR to make it easier to see changes. #5886
KATT commentedon Jul 13, 2024
We're looking for feedback on this sort of return result: https://github.com/trpc/trpc/pull/5886/files#r1676806480
BeBoRE commentedon Jul 16, 2024
@jonahsnider What do you think of this type of return result? https://www-git-fork-bebore-subscription-connection-state-trpc.vercel.app/docs/client/react/useSubscription. Would this suit your needs? Feedback would be appreciated if you could spare the time.
jonahsnider commentedon Jul 18, 2024
This looks great!
UseSubscriptionResulthas all the info I would need to display a status indicator in my app.BeBoRE commentedon Jul 22, 2024
If you want to test the new hook result, you can install the 11.0.0-alpha-tmp-subscription-connection-state.486 version of the client and react package. Documentation is available here https://www-git-subscription-connection-state-trpc.vercel.app/docs/client/react/useSubscription.
github-actions commentedon Oct 22, 2024
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.