Skip to content

Commit

Permalink
add parameter retryUpgradeWebSocket to useChatSession
Browse files Browse the repository at this point in the history
Signed-off-by: San Nguyen <[email protected]>
  • Loading branch information
sandangel committed Oct 11, 2024
1 parent 921c753 commit 8a0df2d
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions libs/react-client/src/useChatSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ const useChatSession = () => {
const _connect = useCallback(
({
userEnv,
accessToken
accessToken,
retryUpgradeWebSocket = false
}: {
userEnv: Record<string, string>;
accessToken?: string;
retryUpgradeWebSocket?: boolean;
}) => {
const { protocol, host, pathname } = new URL(client.httpEndpoint);
const uri = `${protocol}//${host}`;
Expand Down Expand Up @@ -119,16 +121,19 @@ const useChatSession = () => {
};
});

// https://socket.io/docs/v4/how-it-works/#upgrade-mechanism
// Retry upgrading to websocket when error
const engine = socket.io.engine;
engine.on('upgradeError', () => {
setTimeout(() => {
socket.removeAllListeners();
socket.close();
_connect({ userEnv, accessToken });
}, 3000);
});
if (retryUpgradeWebSocket) {
// https://socket.io/docs/v4/how-it-works/#upgrade-mechanism
// Retry upgrading to websocket when error
// This happens sometimes when user is using soft session affinity (like Istio)
const engine = socket.io.engine;
engine.on('upgradeError', () => {
setTimeout(() => {
socket.removeAllListeners();
socket.close();
_connect({ userEnv, accessToken });
}, 3000);
});
}

socket.on('connect', () => {
socket.emit('connection_successful');
Expand Down

0 comments on commit 8a0df2d

Please sign in to comment.