Trying to integrate authentication into socketio websockets has presented a challenge involving the management of auth tokens. The issue revolves around the fact that the function used to retrieve an updated auth token is asynchronous. It appears that providing an async
function to the socketio listener for the "reconnect_attempt"
event does not wait for the function to complete before initiating the reconnect process.
Essentially, this piece of code:
socket.on("reconnect_attempt", async () => {
const token = await getIdToken();
socket.io.opts.query = { token };
});
fails to set the socket's query.token
prior to sending the reconnect request to the server. Consequently, when a reconnection is attempted after a token expiration, the expired token gets sent while the new one is still being fetched.
Is there a way to configure socketio to ensure that it waits for the completion of the listener's handler function before triggering the reconnect request? If not, I might need to take a proactive approach by refreshing the token beforehand and storing it within the app. However, I'm curious if there is another workaround for this scenario.