@@ -32,6 +32,9 @@ export interface WebSocketEnvironment {
3232 workaround ?: string
3333}
3434
35+ /**
36+ * Utilities for creating WebSocket instances across runtimes.
37+ */
3538export class WebSocketFactory {
3639 private static detectEnvironment ( ) : WebSocketEnvironment {
3740 if ( typeof WebSocket !== 'undefined' ) {
@@ -115,6 +118,15 @@ export class WebSocketFactory {
115118 }
116119 }
117120
121+ /**
122+ * Returns the best available WebSocket constructor for the current runtime.
123+ *
124+ * @example
125+ * ```ts
126+ * const WS = WebSocketFactory.getWebSocketConstructor()
127+ * const socket = new WS('wss://realtime.supabase.co/socket')
128+ * ```
129+ */
118130 public static getWebSocketConstructor ( ) : typeof WebSocket {
119131 const env = this . detectEnvironment ( )
120132 if ( env . constructor ) {
@@ -127,11 +139,29 @@ export class WebSocketFactory {
127139 throw new Error ( errorMessage )
128140 }
129141
142+ /**
143+ * Creates a WebSocket using the detected constructor.
144+ *
145+ * @example
146+ * ```ts
147+ * const socket = WebSocketFactory.createWebSocket('wss://realtime.supabase.co/socket')
148+ * ```
149+ */
130150 public static createWebSocket ( url : string | URL , protocols ?: string | string [ ] ) : WebSocketLike {
131151 const WS = this . getWebSocketConstructor ( )
132152 return new WS ( url , protocols )
133153 }
134154
155+ /**
156+ * Detects whether the runtime can establish WebSocket connections.
157+ *
158+ * @example
159+ * ```ts
160+ * if (!WebSocketFactory.isWebSocketSupported()) {
161+ * console.warn('Falling back to long polling')
162+ * }
163+ * ```
164+ */
135165 public static isWebSocketSupported ( ) : boolean {
136166 try {
137167 const env = this . detectEnvironment ( )
0 commit comments