Skip to content

bug: useSubscription never calls onConnectionStateChange #6693

Closed

Description

@darkobits

Provide environment information

System:
    OS: macOS 15.4
    CPU: (10) arm64 Apple M1 Max
    Memory: 7.85 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.11.0 - ~/.nvm/versions/node/v22.11.0/bin/node
    npm: 11.2.0 - ~/.nvm/versions/node/v22.11.0/bin/npm
    Watchman: 2024.11.04.00 - /opt/homebrew/bin/watchman
  Browsers:
    Brave Browser: 133.1.75.181
    Chrome: 135.0.7049.41
    Safari: 18.4
    Safari Technology Preview: 18.4
  npmPackages:
    @tanstack/react-query: ^5.67.1 => 5.72.2 
    @trpc/client: ^11.0.0 => 11.0.4 
    @trpc/server: ^11.0.0 => 11.0.4 
    next: ^15.0.0 => 15.3.0 
    react: ^19.0.0 => 19.1.0 
    typescript: ^5.8.2 => 5.8.3

Describe the bug

When using the useSubscription hook from the new @trpc/tanstack-react-query integration, the onConnectionStateChange callback never fires. However, the subscription.status value does update as expected.

I believe this is happening because the useSubscription hook implementation does not appear to be calling the user-provided onConnectionStateChange callback. See L202, where updateState is being called in a similar fashion as onStarted / onData / onError. However, in these other cases, there is a call to the user-provided callback in addition to updateState:

optsRef.current.onError?.(error);
updateState((prev) => {
  // ...
})

However, in the onConnectionStateChange block, it looks like we are missing this call, which should appear on line 203:

optsRef.current.onConnectionStateChange?.(result);

In fact, there does not appear to be any reference to optsRef.current.onConnectionStateChange anywhere in the hook implementation.

Link to reproduction

https://stackblitz.com/github/trpc/examples-next-minimal-starter

To reproduce

  1. Provide a onConnectionStateChange callback to subscriptionOptions when creating a subscription.
  2. Take any action that would normally trigger a connection state change, such as shutting down the local development server.
  3. Observe that subscription.status (as returned from the useSubscription hook) will update to reflect this change (likely because the hook is calling updateState) but onConnectionStateChange is never called.

Additional information

No response

👨‍👧‍👦 Contributing

  • 🙋‍♂️ Yes, I'd be down to file a PR fixing this bug!

Activity

  1. added
    🙋‍♂️ help wanted
    on Apr 11, 2025
  2. KATT commented on Apr 11, 2025

    @KATT
    Member

    Wanna have a stab at testing it?

    First step is probably to add a regression test inside of https://github.com/trpc/trpc/blob/main/packages/tanstack-react-query/test/useSubscription.test.tsx

    The testReactResource should have a function to .destroyConnections() which should trigger the clients to reconnect

  3. added a commit that references this issue on May 30, 2025
    aabc35a
  4. github-actions commented on Jun 1, 2025

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

  5. locked as resolved and limited conversation to collaborators on Jun 1, 2025
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions