react-signalr icon indicating copy to clipboard operation
react-signalr copied to clipboard

Expose connection state

Open florimm opened this issue 4 years ago • 8 comments

Hi Is it possible to expose also the state of connection so we can use that to know when we are connected /disconnected ?

florimm avatar Aug 26 '20 17:08 florimm

Thanks for your feedback, I will look into it when I have the time.

known-as-bmf avatar Aug 26 '20 22:08 known-as-bmf

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

arung86 avatar Sep 09 '20 03:09 arung86

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.

known-as-bmf avatar Sep 09 '20 08:09 known-as-bmf

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

known-as-bmf avatar Apr 12 '21 15:04 known-as-bmf

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.

edwardmcleodjones avatar Apr 13 '21 09:04 edwardmcleodjones

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.

known-as-bmf avatar Apr 13 '21 10:04 known-as-bmf

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]

known-as-bmf avatar Apr 18 '21 16:04 known-as-bmf

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.

rickyoleary avatar Sep 09 '21 08:09 rickyoleary