@@ -81,6 +81,7 @@ export type RealtimeClientOptions = {
81
81
transport ?: WebSocketLikeConstructor
82
82
timeout ?: number
83
83
heartbeatIntervalMs ?: number
84
+ heartbeatCallback ?: ( status : HeartbeatStatus ) => void
84
85
logger ?: Function
85
86
encode ?: Function
86
87
decode ?: Function
@@ -158,6 +159,7 @@ export default class RealtimeClient {
158
159
* @param options.params The optional params to pass when connecting.
159
160
* @param options.headers Deprecated: headers cannot be set on websocket connections and this option will be removed in the future.
160
161
* @param options.heartbeatIntervalMs The millisec interval to send a heartbeat message.
162
+ * @param options.heartbeatCallback The optional function to handle heartbeat status.
161
163
* @param options.logger The optional function for specialized logging, ie: logger: (kind, msg, data) => { console.log(`${kind}: ${msg}`, data) }
162
164
* @param options.logLevel Sets the log level for Realtime
163
165
* @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 {
421
423
*/
422
424
async sendHeartbeat ( ) {
423
425
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
+ }
425
431
return
426
432
}
427
433
@@ -432,7 +438,11 @@ export default class RealtimeClient {
432
438
'transport' ,
433
439
'heartbeat timeout. Attempting to re-establish connection'
434
440
)
435
- this . heartbeatCallback ( 'timeout' )
441
+ try {
442
+ this . heartbeatCallback ( 'timeout' )
443
+ } catch ( e ) {
444
+ this . log ( 'error' , 'error in heartbeat callback' , e )
445
+ }
436
446
437
447
// Force reconnection after heartbeat timeout
438
448
this . _wasManualDisconnect = false
@@ -454,7 +464,11 @@ export default class RealtimeClient {
454
464
payload : { } ,
455
465
ref : this . pendingHeartbeatRef ,
456
466
} )
457
- this . heartbeatCallback ( 'sent' )
467
+ try {
468
+ this . heartbeatCallback ( 'sent' )
469
+ } catch ( e ) {
470
+ this . log ( 'error' , 'error in heartbeat callback' , e )
471
+ }
458
472
459
473
this . _setAuthSafely ( 'heartbeat' )
460
474
}
@@ -545,7 +559,11 @@ export default class RealtimeClient {
545
559
this . decode ( rawMessage . data , ( msg : RealtimeMessage ) => {
546
560
// Handle heartbeat responses
547
561
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
+ }
549
567
}
550
568
551
569
// Handle pending heartbeat reference cleanup
@@ -853,7 +871,7 @@ export default class RealtimeClient {
853
871
options ?. heartbeatIntervalMs ?? CONNECTION_TIMEOUTS . HEARTBEAT_INTERVAL
854
872
this . worker = options ?. worker ?? false
855
873
this . accessToken = options ?. accessToken ?? null
856
-
874
+ this . heartbeatCallback = options ?. heartbeatCallback ?? noop
857
875
// Handle special cases
858
876
if ( options ?. params ) this . params = options . params
859
877
if ( options ?. logger ) this . logger = options . logger
0 commit comments