Skip to content

Commit 1294997

Browse files
authored
fix: add hearthbeat callback (#529)
1 parent d1d3dfd commit 1294997

File tree

4 files changed

+386
-6
lines changed

4 files changed

+386
-6
lines changed

packages/core/realtime-js/src/RealtimeClient.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export type RealtimeClientOptions = {
8181
transport?: WebSocketLikeConstructor
8282
timeout?: number
8383
heartbeatIntervalMs?: number
84+
heartbeatCallback?: (status: HeartbeatStatus) => void
8485
logger?: Function
8586
encode?: Function
8687
decode?: Function
@@ -158,6 +159,7 @@ export default class RealtimeClient {
158159
* @param options.params The optional params to pass when connecting.
159160
* @param options.headers Deprecated: headers cannot be set on websocket connections and this option will be removed in the future.
160161
* @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
162+
* @param options.heartbeatCallback The optional function to handle heartbeat status.
161163
* @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
162164
* @param options.logLevel Sets the log level for Realtime
163165
* @param options.encode The function to encode outgoing messages. Defaults to JSON: (payload, callback) => callback(JSON.stringify(payload))
@@ -421,7 +423,11 @@ export default class RealtimeClient {
421423
*/
422424
async sendHeartbeat() {
423425
if (!this.isConnected()) {
424-
this.heartbeatCallback('disconnected')
426+
try {
427+
this.heartbeatCallback('disconnected')
428+
} catch (e) {
429+
this.log('error', 'error in heartbeat callback', e)
430+
}
425431
return
426432
}
427433

@@ -432,7 +438,11 @@ export default class RealtimeClient {
432438
'transport',
433439
'heartbeat timeout. Attempting to re-establish connection'
434440
)
435-
this.heartbeatCallback('timeout')
441+
try {
442+
this.heartbeatCallback('timeout')
443+
} catch (e) {
444+
this.log('error', 'error in heartbeat callback', e)
445+
}
436446

437447
// Force reconnection after heartbeat timeout
438448
this._wasManualDisconnect = false
@@ -454,7 +464,11 @@ export default class RealtimeClient {
454464
payload: {},
455465
ref: this.pendingHeartbeatRef,
456466
})
457-
this.heartbeatCallback('sent')
467+
try {
468+
this.heartbeatCallback('sent')
469+
} catch (e) {
470+
this.log('error', 'error in heartbeat callback', e)
471+
}
458472

459473
this._setAuthSafely('heartbeat')
460474
}
@@ -545,7 +559,11 @@ export default class RealtimeClient {
545559
this.decode(rawMessage.data, (msg: RealtimeMessage) => {
546560
// Handle heartbeat responses
547561
if (msg.topic === 'phoenix' && msg.event === 'phx_reply') {
548-
this.heartbeatCallback(msg.payload.status === 'ok' ? 'ok' : 'error')
562+
try {
563+
this.heartbeatCallback(msg.payload.status === 'ok' ? 'ok' : 'error')
564+
} catch (e) {
565+
this.log('error', 'error in heartbeat callback', e)
566+
}
549567
}
550568

551569
// Handle pending heartbeat reference cleanup
@@ -853,7 +871,7 @@ export default class RealtimeClient {
853871
options?.heartbeatIntervalMs ?? CONNECTION_TIMEOUTS.HEARTBEAT_INTERVAL
854872
this.worker = options?.worker ?? false
855873
this.accessToken = options?.accessToken ?? null
856-
874+
this.heartbeatCallback = options?.heartbeatCallback ?? noop
857875
// Handle special cases
858876
if (options?.params) this.params = options.params
859877
if (options?.logger) this.logger = options.logger

0 commit comments

Comments
 (0)