anycable-client icon indicating copy to clipboard operation
anycable-client copied to clipboard

Allow completely resetting the created cable

Open d4rky-pl opened this issue 7 months ago • 1 comments

Use case

When building a native app with log-in screen we may create the cable connection early, e.g. as part of GraphQL subscription initialization. This connection may then either fail to load as it will be unable to generate JWT or it may log-in as not logged in user depending on the server configuration.

The problem is when we then log-in as the user. There's no easy way to reset or recreate the Cable instance resulting in a subtle bug where the first login to the app does not work as expected. Similar problem happens when logging out and logging back in again as the connection has already been closed by the user.

The way we tried to work around this was by triggering cable.cable.connect() when user signs in but it comes with another subtle issue - the initial flag is no longer true so this code snippet from README no longer works as expected:

  transportConfigurator: async (transport, { initial }) => {
    // The initial flag indicates whether this is the first connetion attempt or a reconnection
    if (!initial) return;

    transport.setURL('ws://example.com/cable')

    let response = await fetch('/token.json')
    let data = await response.json()

    transport.setToken(data['token'], 'token')
  }

This can be worked around by manually checking that jid is missing or set to null in the transport.url but it would be great to have some built-in option that doesn't rely on this type of heuristic.

d4rky-pl avatar May 22 '25 13:05 d4rky-pl

There's no easy way to reset or recreate the Cable instance resulting in a subtle bug where the first login to the app does not work as expected

I wonder if we should just re-create the cable instance complete, I mean, call cable = createCable(). This is the best way to start from scratch; trying to reset the state hides many potential caveats.

palkan avatar May 30 '25 20:05 palkan