You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The requirement to use ChannelProvider in Ably-js v2 is kind of annoying because you need to ensure you couple each call to useChannel with a corresponding ChannelProvider.
What's the technical reason ChannelProvider is required? Could we pass all the required information into the hook?
The ChannelProvider component was introduced to avoid issues with inconsistent channel configurations and potential errors arising from them. In ably-js v1, using the useChannel or usePresence hooks with the options parameter could lead to errors when using hooks with the same channel name but different options, or when attempting to dynamically change options for a channel. Here are some older issues related to this problem that I found quickly: #1570, #1477.
For example, code setups like this would fail in v1:
// App.jsexportconstApp=()=>{return(<AblyProviderclient={client}><ConnectedUsers/><LastMessage/></AblyProvider>)}// ConnectedUsers.jsexportconstConnectedUsers=()=>{// usePresence creates channel without optionsconst{ presenceData, updateStatus }=usePresence("my-channel");return(<div>{presenceData.map((msg,index)=><likey={index}>{msg.clientId}: {msg.data}</li>)}</div>)}// LastMessage.jsexportconstLastMessage=()=>{const[lastMessage,setLastMessage]=useState();// `useChannel` here uses the same channel name but tries to set channel options, which would throw an error because this channel has already been created without optionsuseChannel({channelName: "my-channel",options: {params: {rewind: '1'}}},(message)=>{setLastMessage(message);});return(<div>{lastMessage}</div>)}
Running this code would throw Error: Channels.get() cannot be used to set channel options that would cause the channel to reattach. Please, use RealtimeChannel.setOptions() instead. due to internal rules on how channels should communicate with backend Realtime services and handle channel option changes.
So, passing options directly in hooks would cause channels to reattach incorrectly or lead to errors based on the order of calls. The ChannelProvider centralizes the definition of channels and their options so they are managed correctly.
The requirement to use ChannelProvider in Ably-js v2 is kind of annoying because you need to ensure you couple each call to useChannel with a corresponding ChannelProvider.
What's the technical reason ChannelProvider is required? Could we pass all the required information into the hook?
┆Issue is synchronized with this Jira Task by Unito
The text was updated successfully, but these errors were encountered: