Skip to content

feat: auto reconnect the terminal #18796

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

Merged
merged 8 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use websocket-ts instead of partysocket
  • Loading branch information
BrunoQuaresma committed Jul 8, 2025
commit c64a936c5e30de33054d531ecc20ae2f93b611d5
2 changes: 1 addition & 1 deletion site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"lodash": "4.17.21",
"lucide-react": "0.474.0",
"monaco-editor": "0.52.0",
"partysocket": "1.1.4",
"pretty-bytes": "6.1.1",
"react": "18.3.1",
"react-color": "2.19.3",
Expand Down Expand Up @@ -121,6 +120,7 @@
"undici": "6.21.2",
"unique-names-generator": "4.7.1",
"uuid": "9.0.1",
"websocket-ts": "2.2.1",
"yup": "1.6.1"
},
"devDependencies": {
Expand Down
23 changes: 8 additions & 15 deletions site/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions site/src/pages/TerminalPage/TerminalPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import {
import { useProxy } from "contexts/ProxyContext";
import { ThemeOverride } from "contexts/ThemeProvider";
import { useEmbeddedMetadata } from "hooks/useEmbeddedMetadata";
// We use partysocket because it provides automatic reconnection
// and is a drop-in replacement for the native WebSocket API.
import { WebSocket } from "partysocket";
import { type FC, useCallback, useEffect, useRef, useState } from "react";
import { Helmet } from "react-helmet-async";
import { useQuery } from "react-query";
Expand All @@ -29,6 +26,13 @@ import { openMaybePortForwardedURL } from "utils/portForward";
import { terminalWebsocketUrl } from "utils/terminal";
import { getMatchingAgentOrFirst } from "utils/workspace";
import { v4 as uuidv4 } from "uuid";
// Use websocket-ts for better WebSocket handling and auto-reconnection.
import {
ConstantBackoff,
type Websocket,
WebsocketBuilder,
WebsocketEvent,
} from "websocket-ts";
import { TerminalAlerts } from "./TerminalAlerts";
import type { ConnectionStatus } from "./types";

Expand Down Expand Up @@ -224,7 +228,7 @@ const TerminalPage: FC = () => {
}

// Hook up terminal events to the websocket.
let websocket: WebSocket | null;
let websocket: Websocket | null;
const disposers = [
terminal.onData((data) => {
websocket?.send(
Expand Down Expand Up @@ -262,9 +266,11 @@ const TerminalPage: FC = () => {
if (disposed) {
return; // Unmounted while we waited for the async call.
}
websocket = new WebSocket(url);
websocket = new WebsocketBuilder(url)
.withBackoff(new ConstantBackoff(1000))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should switch this to exponential (ideally with some jitter) soon, if not in this PR. A fixed reconnect interval is a good way to DDoS yourself 😆

.build();
websocket.binaryType = "arraybuffer";
websocket.addEventListener("open", () => {
websocket.addEventListener(WebsocketEvent.open, () => {
// Now that we are connected, allow user input.
terminal.options = {
disableStdin: false,
Expand All @@ -281,18 +287,18 @@ const TerminalPage: FC = () => {
);
setConnectionStatus("connected");
});
websocket.addEventListener("error", () => {
websocket.addEventListener(WebsocketEvent.error, () => {
terminal.options.disableStdin = true;
terminal.writeln(
`${Language.websocketErrorMessagePrefix}socket errored`,
);
setConnectionStatus("disconnected");
});
websocket.addEventListener("close", () => {
websocket.addEventListener(WebsocketEvent.close, () => {
terminal.options.disableStdin = true;
setConnectionStatus("disconnected");
});
websocket.addEventListener("message", (event) => {
websocket.addEventListener(WebsocketEvent.message, (_, event) => {
if (typeof event.data === "string") {
// This exclusively occurs when testing.
// "jest-websocket-mock" doesn't support ArrayBuffer.
Expand Down
Loading