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

Fix React Hooks send double ATTACH message action and get doubled rewind messages when using options on v2 #1704

Open
VeskeR opened this issue Mar 20, 2024 · 0 comments
Labels
bug Something isn't working. It's clear that this does need to be fixed.

Comments

@VeskeR
Copy link
Contributor

VeskeR commented Mar 20, 2024

When using ChannelProvider with any sort of options:

<ChannelProvider channelName="my-channel-rewind" options={options}>
  <Component></Component>
</ChannelProvider>

It causes channel to send double ATTACH messages:
image

If using any rewind options { params: { rewind: '1' } }, this then leads to us getting last N rewind messages twice, so we end up with duplicates.
Issue appeared on v2 after transitioning to ChannelProvider implementation in #1620, merge commit ec95b59

This is caused when ChannelProvider calls .setOptions() on the channel in its useEffect hook

channel.setOptions(channelOptionsWithAgent(options));
when channel already started attachment process just before that from within useChannel hook (for example, providing a listener to useChannel hook causes it to attach). Calling .setOptions() in this case triggers reattachment and for some reason causes double ATTACH message being sent, both with the same rewind options (even though first attachment should've been done without any options - they were not set yet).
This is probably an underlying issue with ably-js itself, it should not send double ATTACH messages like in this case. Probably the expected behaviour would be to send first ATTACH message without any options (triggered from useChannel hook when no options are set), and then a second ATTACH message with options from .setOptions() call.

┆Issue is synchronized with this Jira Bug by Unito

@VeskeR VeskeR added the bug Something isn't working. It's clear that this does need to be fixed. label Mar 20, 2024
@VeskeR VeskeR changed the title Fix React Hooks send double ATTACH message action and get doubled rewind messages when using options on v2 Fix React Hooks send double ATTACH message action and get doubled rewind messages when using options on v2 Mar 20, 2024
VeskeR added a commit that referenced this issue Mar 20, 2024
This is a temporary fix for #1704. This does not fix the underlying
problem of ably-js sending two `ATTACH` messages with the same options
when we call `.setOptions()` in `ChannelProvider`.

What `useLayoutEffect` accomplishes is it runs before children
components' `useEffect` hooks, thus it sets options for a channel before
attachment process starts, so there will be no need for reattachment
and sending a second `ATTACH` message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working. It's clear that this does need to be fixed.
Development

No branches or pull requests

1 participant