-
Notifications
You must be signed in to change notification settings - Fork 8
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
Feature request for React Hooks: Handle Spaces initialization in SpacesProvider #265
Comments
hey @mms-uret thanks for the suggestion. Did you run into issues with: const ably = new Realtime.Promise({ key: "VS4Rww.ijkxYg:*****", clientId: 'clemons' });
const spaces = new Spaces(ably); being called too often? I'll forward your proposal to the product team. An alternative way to share the client is also using the |
Hey @dpiatek ! Yes, we actually raised a support ticket with Ably because we couldn't figure out our own mistake. The solution was pretty simple: - const ably = new Realtime.Promise({ key: ablyKey, clientId: user.email });
- const spaces = new Spaces(ably);
+ const spaces = useMemo(() => {
+ const ably = new Realtime.Promise({ key: ablyKey, clientId: user.email });
+ return new Spaces(ably);
+ }, [ablyKey, user.email]); (we're using the |
Ah, I didn't know about <AblyProvider>
<SpacesProvider>
<SpaceProvider>
<App/>
</SpaceProvider>
</SpacesProvider>
</AblyProvider> There is still something needed to initialize the Spaces instance, I think.... |
@mms-uret you can see how we use the providers in our examples repo https://github.com/ably-labs/realtime-examples/blob/main/examples/vite-component-locking/src/index.tsx. It's useful however to get your feedback here. The reason the providers are separated is to ensure compatibility with existing apps which already initialise client and future Ably libraries that could share the connection/client. There might be a case, however, as you point out, to just combine that into one component. |
When the
<SpacesProvider>
React component is used, most of the time, the spaces object needs to be created first.From the documentation:
It would be nice if, alternatively to the spaces object, the
key
and theclientId
could be passed to the SpacesProvider and the SpacesProvider cares about initializing Ably and Spaces:Or alternatively have an object, which is passed to Realtime.Promise().
This would save some boilerplate code and also would make sure that implementers don't have to remember to put the Spaces initialization into a
useMemo()
/useEffect()
hook ;)The text was updated successfully, but these errors were encountered: