1
- import { WebSocket } from 'isows '
1
+ import WebSocketFactory , { WebSocketLike } from './lib/websocket-factory '
2
2
3
3
import {
4
4
CHANNEL_EVENTS ,
@@ -69,8 +69,6 @@ export interface WebSocketLikeConstructor {
69
69
) : WebSocketLike
70
70
}
71
71
72
- export type WebSocketLike = WebSocket
73
-
74
72
export interface WebSocketLikeError {
75
73
error : any
76
74
message : string
@@ -197,17 +195,19 @@ export default class RealtimeClient {
197
195
198
196
this . _setConnectionState ( 'connecting' )
199
197
this . _setAuthSafely ( 'connect' )
200
-
198
+
201
199
// Establish WebSocket connection
202
200
if ( ! this . transport ) {
203
- this . transport = WebSocket
204
- }
205
- if ( ! this . transport ) {
206
- this . _setConnectionState ( 'disconnected' )
207
- throw new Error ( 'No transport provided' )
201
+ try {
202
+ this . conn = WebSocketFactory . createWebSocket ( this . endpointURL ( ) )
203
+ } catch ( error ) {
204
+ this . _setConnectionState ( 'disconnected' )
205
+ throw new Error ( `WebSocket not available: ${ ( error as Error ) . message } ` )
206
+ }
207
+ } else {
208
+ // Use custom transport if provided
209
+ this . conn = new this . transport ! ( this . endpointURL ( ) ) as WebSocketLike
208
210
}
209
-
210
- this . conn = new this . transport ! ( this . endpointURL ( ) ) as WebSocketLike
211
211
this . _setupConnectionHandlers ( )
212
212
}
213
213
@@ -234,7 +234,7 @@ export default class RealtimeClient {
234
234
}
235
235
236
236
this . _setConnectionState ( 'disconnecting' , true )
237
-
237
+
238
238
if ( this . conn ) {
239
239
// Setup fallback timer to prevent hanging in disconnecting state
240
240
const fallbackTimer = setTimeout ( ( ) => {
@@ -413,11 +413,11 @@ export default class RealtimeClient {
413
413
'heartbeat timeout. Attempting to re-establish connection'
414
414
)
415
415
this . heartbeatCallback ( 'timeout' )
416
-
416
+
417
417
// Force reconnection after heartbeat timeout
418
418
this . _wasManualDisconnect = false
419
419
this . conn ?. close ( WS_CLOSE_NORMAL , 'heartbeat timeout' )
420
-
420
+
421
421
setTimeout ( ( ) => {
422
422
if ( ! this . isConnected ( ) ) {
423
423
this . reconnectTimer ?. scheduleTimeout ( )
@@ -435,7 +435,7 @@ export default class RealtimeClient {
435
435
ref : this . pendingHeartbeatRef ,
436
436
} )
437
437
this . heartbeatCallback ( 'sent' )
438
-
438
+
439
439
this . _setAuthSafely ( 'heartbeat' )
440
440
}
441
441
@@ -462,10 +462,16 @@ export default class RealtimeClient {
462
462
if ( customFetch ) {
463
463
_fetch = customFetch
464
464
} else if ( typeof fetch === 'undefined' ) {
465
+ // Node.js environment without native fetch
465
466
_fetch = ( ...args ) =>
466
- import ( '@supabase/node-fetch' as any ) . then ( ( { default : fetch } ) =>
467
- fetch ( ...args )
468
- )
467
+ import ( '@supabase/node-fetch' as any )
468
+ . then ( ( { default : fetch } ) => fetch ( ...args ) )
469
+ . catch ( ( error ) => {
470
+ throw new Error (
471
+ `Failed to load @supabase/node-fetch: ${ error . message } . ` +
472
+ `This is required for HTTP requests in Node.js environments without native fetch.`
473
+ )
474
+ } )
469
475
} else {
470
476
_fetch = fetch
471
477
}
@@ -548,8 +554,6 @@ export default class RealtimeClient {
548
554
} )
549
555
}
550
556
551
-
552
-
553
557
/**
554
558
* Clear specific timer
555
559
* @internal
@@ -579,7 +583,11 @@ export default class RealtimeClient {
579
583
private _setupConnectionHandlers ( ) : void {
580
584
if ( ! this . conn ) return
581
585
582
- this . conn . binaryType = 'arraybuffer'
586
+ // Set binary type if supported (browsers and most WebSocket implementations)
587
+ if ( 'binaryType' in this . conn ) {
588
+ ; ( this . conn as any ) . binaryType = 'arraybuffer'
589
+ }
590
+
583
591
this . conn . onopen = ( ) => this . _onConnOpen ( )
584
592
this . conn . onerror = ( error : Event ) => this . _onConnError ( error )
585
593
this . conn . onmessage = ( event : any ) => this . _onConnMessage ( event )
@@ -798,7 +806,6 @@ export default class RealtimeClient {
798
806
}
799
807
}
800
808
801
-
802
809
/**
803
810
* Setup reconnection timer with proper configuration
804
811
* @internal
@@ -814,8 +821,6 @@ export default class RealtimeClient {
814
821
} , this . reconnectAfterMs )
815
822
}
816
823
817
-
818
-
819
824
/**
820
825
* Initialize client options with defaults
821
826
* @internal
0 commit comments