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

Detach channel in ChannelProvider #1795

Open
ttypic opened this issue Jun 19, 2024 · 5 comments
Open

Detach channel in ChannelProvider #1795

ttypic opened this issue Jun 19, 2024 · 5 comments
Labels
enhancement New feature or improved functionality.

Comments

@ttypic
Copy link
Collaborator

ttypic commented Jun 19, 2024

We should detach channel if there is no ChannelProvider associated with it.

The reason for doing this is that the SDK user should be able to detach from the channel to stop receiving messages from it. In react-hooks, we fully control the channel lifecycle. Automatic detachment seems more reasonable and better than manual detachment using the Ably client. The best place to do this is inside ChannelProvider because this component indicates the intent to use the channel. If it no longer exists, it means we no longer need this channel attached.

┆Issue is synchronized with this Jira Task by Unito

@VeskeR VeskeR added the enhancement New feature or improved functionality. label Jun 20, 2024
@ianb-pomelo
Copy link

Is there a shorter term mitigation for this? We switch our channels somewhat regularly and without this our channels are never detached and have hit the 200 channel limit even though most are unused/unsubscribed (and are also billed for the channels)

@ttypic
Copy link
Collaborator Author

ttypic commented Aug 28, 2024

It depends on what you want to achieve. Based on your business logic, you can invoke the detach() method when you no longer need the channel. Additionally, you can create a wrapper around the ChannelProvider:

const DetachableChannelProvider = (channelProviderProps) => {
   const ablyClient = useAbly();
   const { channelName } = channelProviderProps;

  useEffect(() => {
      // it's better to explicitly attach to the channel in this case
      ablyClient.channels.get(channelName).attach();
      return () => ablyClient.channels.get(channelName).detach();
  }, [channelName])

  return <ChannelProvider {...channelProviderProps} />;
}

@ianb-pomelo
Copy link

Thanks! I've tried to add this but occasionally get an error when switching channels quickly that I can't seem to resolve.

The error is

useChannel.js:56 Uncaught (in promise) Error: Channel detached
    at _RealtimeChannel.processMessage (ably.js:6240:81)
    at Channels2.processChannelMessage (ably.js:6657:19)
    at _ConnectionManager.processChannelMessage (ably.js:5627:34)
    at _ConnectionManager.processNextPendingChannelMessage (ably.js:5613:12)
    at _ConnectionManager.onChannelMessage (ably.js:5606:12)
    at WebSocketTransport.onProtocolMessage (ably.js:4042:32)
    at WebSocketTransport.onWsData (ably.js:7354:12)
    at wsConnection.onmessage (ably.js:7308:19)

which seems to be caused by attempting to re-subscribe to a channel that is detaching/detached. Is there anything I can do to handle this? It seems like the handleChannelMount is throwing but because it's in an effect I don't think I can handle it

@ttypic
Copy link
Collaborator Author

ttypic commented Sep 10, 2024

Hey @ianb-pomelo,

I apologize for the delay. I missed the notification about your message. Could you please attach verbose logs or share a reproducible example? I’m having difficulty reproducing the issue you’re facing.

@zameschua
Copy link

Hey @ttypic

I'm guessing it's related to this issue #1594

Seems like the steps to reproduce and fix the bug are already in this other PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improved functionality.
Development

No branches or pull requests

4 participants