Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SignalR][Client][TS] Unable to unsubscribe from SignalR connection state change events #59626

Open
1 task done
imekachi opened this issue Dec 24, 2024 · 0 comments
Open
1 task done
Labels
area-signalr Includes: SignalR clients and servers

Comments

@imekachi
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I am integrating SignalR into my React code. Currently, there is no way to listen to all connection state changes.
A workaround I found is to use onclose, onreconnecting and onreconnected. However, there is no way to unsubscribe from these events.

On the client code, I preserve and reuse the connection instance. When a component needs to access the SignalR connection, it will get the same connection instance if one has already been created, and then subscribe to the changes.
When the component unmounts, it has no way to unsubscribe so the stale handlers are stuck in the connection instance(this._closedCallbacks, this._reconnectingCallbacks, and this._reconnectedCallbacks). When the component is remounted, new handlers are added. This causes memory leak on the client side.

Describe the solution you'd like

  • Add a way to unsubscribe from connection status change events. For example,
    • Returns the unsubscribe function when calling onclose, onreconnecting, and onreconnected (Preferred, the consumer doesn't have to cache the callback reference manually).
    • Or provides offclose, offreconnecting, and offreconnected (the naming can be changed).
  • Or add a new method onConnectionStateChange which will be fired every time this._connectionState changes and do one of the following,
    • Returns the unsubscribe function (Preferred, the consumer doesn't have to cache the callback reference manually).
    • Or provides offConnectionStateChange (the naming can be changed).

Additional context

The issue can be addressed by updating this file src/SignalR/clients/ts/signalr/src/HubConnection.ts

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-signalr Includes: SignalR clients and servers label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-signalr Includes: SignalR clients and servers
Projects
None yet
Development

No branches or pull requests

1 participant