@@ -49,7 +49,17 @@ type RealtimeRequestAuthenticate = {
49
49
50
50
type Realtime = {
51
51
socket?: WebSocket;
52
+
53
+ /**
54
+ * Timeout for reconnect operations.
55
+ */
52
56
timeout?: number;
57
+
58
+ /**
59
+ * Heartbeat interval for the realtime connection.
60
+ */
61
+ heartbeat?: number;
62
+
53
63
url?: string;
54
64
lastMessage?: RealtimeResponse;
55
65
channels: Set<string >;
@@ -63,6 +73,7 @@ type Realtime = {
63
73
getTimeout: () => number;
64
74
connect: () => void;
65
75
createSocket: () => void;
76
+ createHeartbeat: () => void;
66
77
cleanUp: (channels: string[]) => void;
67
78
onMessage: (event: MessageEvent) => void;
68
79
}
@@ -174,6 +185,7 @@ class Client {
174
185
private realtime: Realtime = {
175
186
socket: undefined,
176
187
timeout: undefined,
188
+ heartbeat: undefined,
177
189
url: '',
178
190
channels: new Set(),
179
191
subscriptions: new Map(),
@@ -199,6 +211,17 @@ class Client {
199
211
return 60_000;
200
212
}
201
213
},
214
+ createHeartbeat: () => {
215
+ if (this.realtime.heartbeat) {
216
+ clearTimeout(this.realtime.heartbeat);
217
+ }
218
+
219
+ this.realtime.heartbeat = window?.setInterval(() => {
220
+ this.realtime.socket?.send(JSON.stringify({
221
+ type: 'ping'
222
+ }));
223
+ }, 20_000);
224
+ },
202
225
createSocket: () => {
203
226
if (this.realtime.channels.size < 1) {
204
227
this.realtime.reconnect = false;
@@ -237,6 +260,7 @@ class Client {
237
260
this.realtime.socket.addEventListener('message', this.realtime.onMessage);
238
261
this.realtime.socket.addEventListener('open', _event => {
239
262
this.realtime.reconnectAttempts = 0;
263
+ this.realtime.createHeartbeat();
240
264
});
241
265
this.realtime.socket.addEventListener('close', event => {
242
266
if (
0 commit comments