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(core): Setup websocket keep-live messages #6866

Merged
merged 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
"[email protected]": "patches/[email protected]",
"@sentry/[email protected]": "patches/@[email protected]",
"[email protected]": "patches/[email protected]",
"[email protected]": "patches/[email protected]"
"[email protected]": "patches/[email protected]",
"@types/[email protected]": "patches/@[email protected]"
}
}
}
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/@[email protected]
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.