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

Bug: Collaboration Plugin does not update the provider when providerFactory is updated. #7136

Open
matsuyama-k1 opened this issue Feb 5, 2025 · 2 comments
Labels
collab Related to Lexical collaboration features

Comments

@matsuyama-k1
Copy link
Contributor

matsuyama-k1 commented Feb 5, 2025

Lexical version: 0.23.1

Steps To Reproduce

  1. Update providerFactory during collaboration.
  2. Provider is not updated.

The current behavior

Provider is not updated by updationg providerFactory.
This behavior caused by this line.

The expected behavior

Provider can be updated when providerFactory is updated.

The problem I hit

I want to update providerFactory every time when id token is updated to prevent connection lost.
So I write the code like below.

const MyCollaborationPlugin = () => {
  const idToken = useIdToken();

  // This function updates triggered by update of token.
  const providerFactoryFunction = useCallback(
    (id: string, yjsDocMap: Map<string, Doc>) => {
      const doc = getDocFromMap(id, yjsDocMap);

      return new WebsocketProvider('wss://test.com', id, doc, {
        connect: false,
        params: {
          id_token: idToken,
          item_id: id,
        },
      });
    },
    [idToken],
  );

  return <CollaborationPlugin providerFactory={providerFactoryFunction} id="dummyId" shouldBootstrap={true} />;
};

However, lexical implementation looks like preventing update provider. So I want to remove the line if it is possible.

Impact of fix

In the current implementation, if the ID token is updated after the WebSocket connection has been established and the connection is subsequently lost, the providerFactory isn’t updated.
As a result, the query parameter for the ID token remains outdated, preventing the connection from being re-established. With solving this issue, updating the ID token will allow the connection to be restored.

@matsuyama-k1
Copy link
Contributor Author

matsuyama-k1 commented Feb 5, 2025

related to #6271

@etrepum etrepum added the collab Related to Lexical collaboration features label Feb 5, 2025
@matsuyama-k1
Copy link
Contributor Author

matsuyama-k1 commented Feb 6, 2025

I found the way to bypass regeneration of provider factory.
I forked y-websocket and changed the behavior of its onClose function to refetch the idToken when it reconnects.

in y-websocket

    websocket.onclose = async (event) => {
      // ...

      if (provider.shouldConnect) {

        // Fetch new id token when WebSocket disconnects.
        const token = await fetchAuthToken();
        if (token) {
          provider.params = {
            ...provider.params,
            id_token: token,
          };
        }

        setTimeout(
          (provider) => {
            setupWS(provider);
          },
          math.min(math.pow(2, provider.wsUnsuccessfulReconnects) * 100, provider.maxBackoffTime),
          provider,
        );
      }
    };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collab Related to Lexical collaboration features
Projects
None yet
Development

No branches or pull requests

2 participants