From d7213c255c8f215bc4a0f5ed3e6aa79c82178cda Mon Sep 17 00:00:00 2001 From: MartinBraquet Date: Thu, 9 Oct 2025 17:50:43 +0200 Subject: [PATCH] Add client side heartbeat --- common/src/api/websocket-client.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/common/src/api/websocket-client.ts b/common/src/api/websocket-client.ts index 74ee83e..28da69d 100644 --- a/common/src/api/websocket-client.ts +++ b/common/src/api/websocket-client.ts @@ -54,7 +54,7 @@ export class APIRealtimeClient { // subscribers by the topic they are subscribed to subscriptions: Map connectTimeout?: NodeJS.Timeout - heartbeat?: NodeJS.Timeout + heartbeat?: number | undefined; constructor(url: string) { this.url = url @@ -90,10 +90,12 @@ export class APIRealtimeClient { if (VERBOSE_LOGGING) { console.info('API websocket opened.') } - this.heartbeat = setInterval( - async () => this.sendMessage('ping', {}).catch(console.error), - 30000 - ) + // Send a heartbeat ping every 25s + this.heartbeat = window.setInterval(() => { + if (this.ws.readyState === WebSocket.OPEN) { + this.ws.send(JSON.stringify({ type: "ping" })); + } + }, 25000); if (this.subscriptions.size > 0) { this.sendMessage('subscribe', { topics: Array.from(this.subscriptions.keys()), @@ -105,7 +107,7 @@ export class APIRealtimeClient { if (VERBOSE_LOGGING) { console.info(`API websocket closed with code=${ev.code}: ${ev.reason}`) } - clearInterval(this.heartbeat) + if (this.heartbeat) clearInterval(this.heartbeat) // mqp: we might need to change how the txn stuff works if we ever want to // implement "wait until i am subscribed, and then do something" in a component.