Skip to content

Commit

Permalink
fix(core): Setup websocket keep-live messages
Browse files Browse the repository at this point in the history
fixes #6757
  • Loading branch information
netroy committed Aug 7, 2023
1 parent 6499f42 commit 88d437a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 17 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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]"
}
}
}
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. */
31 changes: 16 additions & 15 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 88d437a

Please sign in to comment.