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

Most upvoted comments

@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:

const reloadAbly = useCallback(() => {
    setClient(prevClient => { 
      setTimeout(() => prevClient.close(), 1000);
      return new Ably.Realtime.Promise({
         authUrl: "/api/ably-token",
         authMethod: "POST"
      });
    });
  }, []);

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)

const reloadAbly = useCallback(() => {
    setClient(prevClient => { 
      prevClient.close();
      return new Ably.Realtime.Promise({
         authUrl: "/api/ably-token",
         authMethod: "POST"
      });
    });
  }, []);

I get errors from prevClient.close(), like this:

Uncaught (in promise) Error: Connection closed
    at new ErrorInfo (ably-commonjs.js:1158:1)
    at ErrorInfo.fromValues (ably-commonjs.js:1175:1)
    at Object.closed (ably-commonjs.js:5176:1)
    at Transport.close (ably-commonjs.js:5517:1)
    at ConnectionManager.closeImpl (ably-commonjs.js:8946:1)
    at ConnectionManager.requestState (ably-commonjs.js:8697:1)
    at Connection.close (ably-commonjs.js:12050:1)
    at Realtime.close (ably-commonjs.js:11857:1)

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 the channel.state is 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 use Realtime.Promise and just use Realtime instead, but I have to use Realtime.Promise to get presenceData updates via the usePresence hook.

Another thing I’m seeing is that when I close the connection I also get a bunch of errors printed to the console:

Unchecked runtime.lastError: The message port closed before a response was received.

and

Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

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!