How to refresh the wsProvider params when token expire

when the jwt token is expired, I want to refresh token on client and reconnect the websocket, this is how I do the logic:

const handleWsAuth = (event: any, wsProvider: WebsocketProvider) => {
    if (event.status === 'failed') {
        wsProvider.shouldConnect = false;
        wsProvider.ws?.close()
    }
    if (event.status === 'expired') {
        RequestHandler.handleWebAccessTokenExpire().then((res) => {
            if (ResponseHandler.responseSuccess(res)) {
                wsProvider.
                wsProvider.shouldConnect = true;
            } else {
                wsProvider.shouldConnect = false;
                wsProvider.ws?.close();
            }
        })
    }
}

Now I am facing a issue is that how to refresh the wsProvider parameters? I am initial the wsProvider like this:

const wsProvider = new WebsocketProvider(readConfig("wssUrl"), docId, ydoc, {
        maxBackoffTime: 1000000,
        params: {
            // https://self-issued.info/docs/draft-ietf-oauth-v2-bearer.html#query-param
            access_token: localStorage.getItem(WheelGlobal.ACCESS_TOKEN_NAME) ?? ""
        }
    });

It’s not possible to give a WebsocketProvider new params after it has been constructed. Your best bet would be to destroy the existing provider and create a new one.

1 Like