use-pusher icon indicating copy to clipboard operation
use-pusher copied to clipboard

is this hooks automatically disconnect channels?

Open 201524547 opened this issue 4 years ago • 8 comments

useChannel hooks automatically disconnect channels?

i use this hooks, i wonder useChannel hook automatically disconnect when component unmount?

201524547 avatar Jan 29 '21 05:01 201524547

Thanks for letting me know. Do you have a codesandbox that reproduces this?

mayteio avatar Jan 29 '21 08:01 mayteio

@201524547 It was something i had reported time ago, it disconnects and never re-connects back to the channel if the component re-renders @mayteio All you have to do is simply have a component that with a parent or some dependency that make it re-render and test having the useChannel with some useEvent and you'll notice the first time pusher invokes your callback its okay but the 2nd time after component gets re-rendered it just unbinds the channel (disconnecting or not) and the callbacks are no longer doing anything

underfisk avatar Feb 07 '21 15:02 underfisk

Please provide a codesandbox - I do not have this issue.

mayteio avatar Feb 09 '21 04:02 mayteio

Sure i'll do that but i think i found why its probably happening, let me explain it quickly There are types of channels that need to be persisted while the application is on, usually those are bounded to the user itself and not to the component, useChannel makes it unsubscribe on unmount and what if a component uses a user room that needs to be alive while the app is running? That was in fact one of the issues, my workaround was creating a custom useChannel that just simply doesn't unsubscribe but removes the event only but i think there is room for improvement. Do you think that considering a parameter option whether we can unsubscribe or not? It does make sense having it unsubscribing on unmount but sometimes you don't want that channel to go away in sub-components, not sure what's your suggestion in here. Once i have some free time i'll publish here the issue

underfisk avatar Feb 09 '21 04:02 underfisk

Hey, why don’t you move the useChannel hook higher up into a provider? That way it can live connected for the duration of the session.

mayteio avatar Feb 09 '21 08:02 mayteio

At some point if my event uses that channel somehow its unsubscribing, i tried that but i can check again What i ended up doing is check if the channel exists and re-use but never close it (only the higher up provider) is closing it but probably i might be doing something wrong

underfisk avatar Feb 09 '21 17:02 underfisk

Might #43 solve this too?

mayteio avatar Feb 25 '21 03:02 mayteio

yeah, I ran into the same problem and (as far as I can tell), #43 would solve this perfectly.

jan-wilhelm avatar Mar 18 '21 18:03 jan-wilhelm