Skip to content

Commit ee36e46

Browse files
fix(realtime): prevent INVALID_STATE_ERROR crash in heartbeat
Use clearInterval instead of clearTimeout when resetting the heartbeat timer, and guard socket.send() with a readyState check so pings are only sent on an open WebSocket. Fixes #101 Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent ff16bab commit ee36e46

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

src/client.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,17 @@ class Client {
420420
}
421421
},
422422
createHeartbeat: () => {
423-
if (this.realtime.heartbeat) {
424-
clearTimeout(this.realtime.heartbeat);
423+
if (this.realtime.heartbeat !== undefined) {
424+
clearInterval(this.realtime.heartbeat);
425+
this.realtime.heartbeat = undefined;
425426
}
426427

427428
this.realtime.heartbeat = window?.setInterval(() => {
428-
this.realtime.socket?.send(JSONbig.stringify({
429-
type: 'ping'
430-
}));
429+
if (this.realtime.socket?.readyState === WebSocket.OPEN) {
430+
this.realtime.socket.send(JSONbig.stringify({
431+
type: 'ping'
432+
}));
433+
}
431434
}, 20_000);
432435
},
433436
createSocket: () => {

0 commit comments

Comments
 (0)