react-signalr
react-signalr copied to clipboard
Expose connection state
Hi Is it possible to expose also the state of connection so we can use that to know when we are connected /disconnected ?
Thanks for your feedback, I will look into it when I have the time.
Hi Is it possible to expose also the state of connection so we can use that to know when we are connected /disconnected ?
Even I am looking for that feature, as it fails to send the data if connection is down
I fiddled around with this and faced some issues with the HubConnection API.
There is no way to subscribe/react to state change at the moment so I had to work around this limitation with some ugly code...
If tou are interested, the code I came up with is here (branch): https://github.com/known-as-bmf/react-signalr/tree/feature/connection_state
relevant code: https://github.com/known-as-bmf/react-signalr/blob/feature/connection_state/src/useSignalr.ts#L71-L108
In this code, I highjack the internal connectionState
class variable so it call an event onstatechange
.
I don't like this code very much and I might open an issue on @microsoft/signalr
so this feature is present natively.
Thinking about it, it may be possible for the hook to return an Observable
(ReplaySubject
?) emitting the state ?
const { state$ } = useSignalr(/* ... */);
Let me think about it...
I think this is pretty close to #4 ?
Maybe we could discuss this there ?
Yeah, sorry for hijacking the other PR. As I said, I added the connection$
into the useSignalr
response so that we could handle onreconnecting
, onreconnected
, onclose
, etc. I had looked at this issue and the connection_state
branch, but you didn't seem too happy with your changes, so went with the simpler approach in the interim.
But returning an Observable of the connection state would be nicer to manage in the consuming code, so I like the idea above.
I created a new branch https://github.com/known-as-bmf/react-signalr/tree/feature/connection_state_stream where I will try to implement this.
I don't really like what the consuming code will look like tho
useEffect(() => {
const subscription = state$.subscribe(state => {
if (state === HubConnectionState.Reconnecting) {
/* ... */
} else if ( /* ... */) {
/* ... */
}
/* ... */
});
return () => subscription.unsubscribe();
}, [state$]);
I also worry about what happen if you add dependencies to the dependency list. The subscription would get "refreshed" when they change and that could create confusion.
I published beta version of the package with the state
returned by the hook. It also includes #9 #10 If someone could test/experiment with it, I would be very grateful.
The version is 1.0.7-beta.0
.
npm i @known-as-bmf/[email protected]
Hi, just thought I'd comment on this, for me it's working fine right now. I needed this to detect when connected to refresh the connection ID client side. I can now hook into the state.Connected state and then call invoke to get the connection ID. This is also called when a reconnection (then connect) happens.