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

fix: socket.io upgrade sometimes happen before connect #1471

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions libs/react-client/src/useChatSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,32 +133,28 @@ const useChatSession = () => {
// https://socket.io/docs/v4/how-it-works/#upgrade-mechanism
// Require WebSocket when connecting to backend
if (requireWebSocket) {
// https://socket.io/docs/v4/client-socket-instance/#socketio
// 'connect' event is emitted when the underlying connection is established with polling transport
// 'upgrade' event is emitted when the underlying connection is upgraded to WebSocket and polling request is stopped.
const engine = socket.io.engine;
// https://github.com/socketio/socket.io/tree/main/packages/engine.io-client#events
engine.once('upgrade', () => {
// Set session on connect event, otherwise user can not interact with text input UI.
// Upgrade event is required to make sure user won't interact with the session before websocket upgrade success
socket.on('connect', onConnect);
});
// Socket.io will not retry upgrade request.
// Retry upgrade to websocket when error can only be done via reconnect.
// This will not be an issue for users if they are using persistent sticky session.
// In case they are using soft session affinity like Istio, then sometimes upgrade request will fail
engine.once('upgradeError', () => {
onConnectError();
socket.io.engine.once('upgradeError', () => {
setTimeout(() => {
socket.removeAllListeners();
socket.close();
_connect({
userEnv,
accessToken,
requireWebSocket
});
_connect({ userEnv, accessToken, requireWebSocket });
}, 500);
});

// https://socket.io/docs/v4/client-socket-instance/#socketio
socket.on('connect', () => {
// Sometimes upgrade request already succeeded before connect event
if (socket.io.engine.transport.name === 'websocket') {
onConnect();
} else {
socket.io.engine.once('upgrade', onConnect);
}
});
} else {
socket.on('connect', onConnect);
}
Expand Down