Skip to content

Commit

Permalink
fix(core): Setup websocket keep-live messages (#6866)
Browse files Browse the repository at this point in the history
fixes #6757
  • Loading branch information
netroy committed Aug 28, 2023
1 parent 60c20a4 commit 8bdb07d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"typedi@0.10.0": "patches/typedi@0.10.0.patch",
"@sentry/cli@2.17.0": "patches/@sentry__cli@2.17.0.patch",
"pkce-challenge@3.0.0": "patches/pkce-challenge@3.0.0.patch",
"pyodide@0.23.4": "patches/pyodide@0.23.4.patch"
"pyodide@0.23.4": "patches/pyodide@0.23.4.patch",
"@types/ws@8.5.4": "patches/@types__ws@8.5.4.patch"
}
}
}
4 changes: 2 additions & 2 deletions packages/cli/src/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Push extends EventEmitter {
} else if (!useWebSockets) {
(this.backend as SSEPush).add(req.query.sessionId, { req, res });
} else {
res.status(1008).send('Unauthorized');
res.status(401).send('Unauthorized');
}
this.emit('editorUiConnected', req.query.sessionId);
}
Expand Down Expand Up @@ -88,7 +88,7 @@ export const setupPushHandler = (restEndpoint: string, app: Application) => {
ws.send(`Unauthorized: ${(error as Error).message}`);
ws.close(1008);
} else {
res.status(1008).send('Unauthorized');
res.status(401).send('Unauthorized');
}
return;
}
Expand Down
33 changes: 32 additions & 1 deletion packages/cli/src/push/websocket.push.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import type WebSocket from 'ws';
import { AbstractPush } from './abstract.push';

function heartbeat(this: WebSocket) {
this.isAlive = true;
}

export class WebSocketPush extends AbstractPush<WebSocket> {
constructor() {
super();

// Ping all connected clients every 60 seconds
setInterval(() => this.pingAll(), 60 * 1000);
}

add(sessionId: string, connection: WebSocket) {
connection.isAlive = true;
connection.on('pong', heartbeat);

super.add(sessionId, connection);

// Makes sure to remove the session if the connection is closed
connection.once('close', () => this.remove(sessionId));
connection.once('close', () => {
connection.off('pong', heartbeat);
this.remove(sessionId);
});
}

protected close(connection: WebSocket): void {
Expand All @@ -16,4 +33,18 @@ export class WebSocketPush extends AbstractPush<WebSocket> {
protected sendToOne(connection: WebSocket, data: string): void {
connection.send(data);
}

private pingAll() {
for (const sessionId in this.connections) {
const connection = this.connections[sessionId];
// If a connection did not respond with a `PONG` in the last 60 seconds, disconnect
if (!connection.isAlive) {
delete this.connections[sessionId];
return connection.terminate();
}

connection.isAlive = false;
connection.ping();
}
}
}
14 changes: 14 additions & 0 deletions patches/@types__ws@8.5.4.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/index.d.ts b/index.d.ts
index 7a8182a94289524851cb08a3b24897f2b6bce747..f5bfb61bdacbae81ca274cc4b5a61e6e7322b7cd 100755
--- a/index.d.ts
+++ b/index.d.ts
@@ -72,6 +72,9 @@ declare class WebSocket extends EventEmitter {
| typeof WebSocket.CLOSED;
readonly url: string;

+ /** Indicates if the connection has replied to the last PING */
+ isAlive: boolean;
+
/** The connection is not yet open. */
readonly CONNECTING: 0;
/** The connection is open and ready to communicate. */
8 changes: 6 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit 8bdb07d

Please sign in to comment.