diff --git a/templates/react-native/src/client.ts.twig b/templates/react-native/src/client.ts.twig index 5f2b423b0d..fefdaba931 100644 --- a/templates/react-native/src/client.ts.twig +++ b/templates/react-native/src/client.ts.twig @@ -309,13 +309,15 @@ class Client { }, createHeartbeat: () => { if (this.realtime.heartbeat) { - clearTimeout(this.realtime.heartbeat); + clearInterval(this.realtime.heartbeat); } this.realtime.heartbeat = window?.setInterval(() => { - this.realtime.socket?.send(JSONbig.stringify({ - type: 'ping' - })); + if (this.realtime.socket?.readyState === WebSocket.OPEN) { + this.realtime.socket.send(JSONbig.stringify({ + type: 'ping' + })); + } }, 20_000); }, createSocket: () => { diff --git a/templates/web/src/client.ts.twig b/templates/web/src/client.ts.twig index d28c114aba..b0a0880b43 100644 --- a/templates/web/src/client.ts.twig +++ b/templates/web/src/client.ts.twig @@ -504,13 +504,15 @@ class Client { }, createHeartbeat: () => { if (this.realtime.heartbeat) { - clearTimeout(this.realtime.heartbeat); + clearInterval(this.realtime.heartbeat); } this.realtime.heartbeat = window?.setInterval(() => { - this.realtime.socket?.send(JSONbig.stringify({ - type: 'ping' - })); + if (this.realtime.socket?.readyState === WebSocket.OPEN) { + this.realtime.socket.send(JSONbig.stringify({ + type: 'ping' + })); + } }, 20_000); }, createSocket: () => {