[SignalR][Client][TS] Unable to unsubscribe from SignalR connection state change events #59626
Open
1 task done
Labels
area-signalr
Includes: SignalR clients and servers
Is there an existing issue for this?
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
andonreconnected
. 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
, andthis._reconnectedCallbacks
). When the component is remounted, new handlers are added. This causes memory leak on the client side.Describe the solution you'd like
onclose
,onreconnecting
, andonreconnected
(Preferred, the consumer doesn't have to cache the callback reference manually).offclose
,offreconnecting
, andoffreconnected
(the naming can be changed).onConnectionStateChange
which will be fired every timethis._connectionState
changes and do one of the following,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
The text was updated successfully, but these errors were encountered: