Skip to content

feat: access subscription connection status from tRPC client #5871

Closed

Description

@jonahsnider

Describe the feature you'd like to request

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!

Activity

  1. KATT commented on Jul 5, 2024

    @KATT
    Member

    useSubscription's return should return some of this info indeed

  2. added
    ✅ accepted-PRs-welcome
    on Jul 5, 2024
  3. BeBoRE commented on Jul 9, 2024

    @BeBoRE
    Contributor

    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.

  4. KATT commented on Jul 9, 2024

    @KATT
    Member

    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 😅

  5. KATT commented on Jul 9, 2024

    @KATT
    Member

    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

  6. BeBoRE commented on Jul 9, 2024

    @BeBoRE
    Contributor

    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.

  7. KATT commented on Jul 9, 2024

    @KATT
    Member

    Not entirely correct - I think we get error events that we just ignore when it leads to a reconnect

    eventSource.addEventListener('error', (cause) => {
    if (eventSource.readyState === EventSource.CLOSED) {
    stream.controller.error(cause);
    }

  8. BeBoRE commented on Jul 9, 2024

    @BeBoRE
    Contributor

    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.

  9. BeBoRE commented on Jul 9, 2024

    @BeBoRE
    Contributor

    I'll create a draft PR to make it easier to see changes. #5886

  10. KATT commented on Jul 13, 2024

    @KATT
    Member
  11. BeBoRE commented on Jul 16, 2024

    @BeBoRE
    Contributor

    @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.

  12. jonahsnider commented on Jul 18, 2024

    @jonahsnider
    Author

    This looks great! UseSubscriptionResult has all the info I would need to display a status indicator in my app.

  13. BeBoRE commented on Jul 22, 2024

    @BeBoRE
    Contributor

    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.

  14. github-actions commented on Oct 22, 2024

    @github-actions
    Contributor

    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.

  15. locked as resolved and limited conversation to collaborators on Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✅ accepted-PRs-welcome

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions