ably-js: Avoiding `clientId mismatch` error when user's session up/downgrades
Currently when user’s clientId changes (e.g. logs in), clientId will go from undefined to “my-user-id”, which I do with
const { user } = useUser();
const { ably } = useAbly();
useEffect(() => {
ably.auth.authorize();
}, [user])
Our custom API endpoint ably calls to get the token will take care of figuring out the clientId/capabilities based on the cookie session.
After the request, however, any subsequent realtime connections break with “clientId mismatch” error.
I would like to know if there’s a way to cleanly cleanup and restart the Ably auth context to avoid this error
┆Issue is synchronized with this Jira Story by Unito
About this issue
- Original URL
- State: open
- Created 3 years ago
- Comments: 21
@thenano Thanks for reporting this! It looks like a bug, and we will fix it. As a workaround for now, you can add a delay for closing, for example:
Hi @ttypic, I can confirm that 2.6.3 has fixed the issue with the connection closed error that was being thrown before. Thanks for you help with this!
Hi @ttypic my bad, it seems to work as expected, when I log in the hooks will re-render and subscriptions will re-establish. I did have another issue, just wanted to clarify implementation here: If I do this (specifically if I use
Realtime.Promise)I get errors from
prevClient.close(), like this:These errors happen when I use
usePresence, and specifically when the component using that hook unmounts, the error comes from leaving the channel, done on this line.. It’s weird that thechannel.stateis still “attached”, but I guess that means it’s not updated it’s state yet. Any ideas on what can be done to not get this error? I’m trying to just use the provided hooks and not have to implement the same functionality. As a note, this does not happen if I do not useRealtime.Promiseand just useRealtimeinstead, but I have to useRealtime.Promiseto get presenceData updates via theusePresencehook.Another thing I’m seeing is that when I close the connection I also get a bunch of errors printed to the console:
and
I’m wondering if I need to do anything differently? Or if there’s a way to cleanly close the connection avoiding all these errors?
thanks!