@supabase/realtime-js 2.13.0 → 2.15.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/main/RealtimeChannel.d.ts.map +1 -1
  2. package/dist/main/RealtimeChannel.js +26 -10
  3. package/dist/main/RealtimeChannel.js.map +1 -1
  4. package/dist/main/RealtimeClient.d.ts +1 -1
  5. package/dist/main/RealtimeClient.d.ts.map +1 -1
  6. package/dist/main/RealtimeClient.js +40 -9
  7. package/dist/main/RealtimeClient.js.map +1 -1
  8. package/dist/main/index.auto.d.ts +11 -0
  9. package/dist/main/index.auto.d.ts.map +1 -0
  10. package/dist/main/index.auto.js +34 -0
  11. package/dist/main/index.auto.js.map +1 -0
  12. package/dist/main/index.d.ts +2 -1
  13. package/dist/main/index.d.ts.map +1 -1
  14. package/dist/main/index.js +3 -1
  15. package/dist/main/index.js.map +1 -1
  16. package/dist/main/lib/constants.d.ts +1 -0
  17. package/dist/main/lib/constants.d.ts.map +1 -1
  18. package/dist/main/lib/constants.js +2 -1
  19. package/dist/main/lib/constants.js.map +1 -1
  20. package/dist/main/lib/timer.d.ts.map +1 -1
  21. package/dist/main/lib/timer.js +1 -0
  22. package/dist/main/lib/timer.js.map +1 -1
  23. package/dist/main/lib/transformers.js +1 -1
  24. package/dist/main/lib/transformers.js.map +1 -1
  25. package/dist/main/lib/version.d.ts +1 -1
  26. package/dist/main/lib/version.js +1 -1
  27. package/dist/main/lib/websocket-factory-auto.d.ts +26 -0
  28. package/dist/main/lib/websocket-factory-auto.d.ts.map +1 -0
  29. package/dist/main/lib/websocket-factory-auto.js +71 -0
  30. package/dist/main/lib/websocket-factory-auto.js.map +1 -0
  31. package/dist/main/lib/websocket-factory.d.ts +35 -0
  32. package/dist/main/lib/websocket-factory.d.ts.map +1 -0
  33. package/dist/main/lib/websocket-factory.js +95 -0
  34. package/dist/main/lib/websocket-factory.js.map +1 -0
  35. package/dist/module/RealtimeChannel.d.ts.map +1 -1
  36. package/dist/module/RealtimeChannel.js +27 -11
  37. package/dist/module/RealtimeChannel.js.map +1 -1
  38. package/dist/module/RealtimeClient.d.ts +1 -1
  39. package/dist/module/RealtimeClient.d.ts.map +1 -1
  40. package/dist/module/RealtimeClient.js +41 -9
  41. package/dist/module/RealtimeClient.js.map +1 -1
  42. package/dist/module/index.auto.d.ts +11 -0
  43. package/dist/module/index.auto.d.ts.map +1 -0
  44. package/dist/module/index.auto.js +13 -0
  45. package/dist/module/index.auto.js.map +1 -0
  46. package/dist/module/index.d.ts +2 -1
  47. package/dist/module/index.d.ts.map +1 -1
  48. package/dist/module/index.js +2 -1
  49. package/dist/module/index.js.map +1 -1
  50. package/dist/module/lib/constants.d.ts +1 -0
  51. package/dist/module/lib/constants.d.ts.map +1 -1
  52. package/dist/module/lib/constants.js +1 -0
  53. package/dist/module/lib/constants.js.map +1 -1
  54. package/dist/module/lib/timer.d.ts.map +1 -1
  55. package/dist/module/lib/timer.js +1 -0
  56. package/dist/module/lib/timer.js.map +1 -1
  57. package/dist/module/lib/transformers.js +1 -1
  58. package/dist/module/lib/transformers.js.map +1 -1
  59. package/dist/module/lib/version.d.ts +1 -1
  60. package/dist/module/lib/version.js +1 -1
  61. package/dist/module/lib/websocket-factory-auto.d.ts +26 -0
  62. package/dist/module/lib/websocket-factory-auto.d.ts.map +1 -0
  63. package/dist/module/lib/websocket-factory-auto.js +67 -0
  64. package/dist/module/lib/websocket-factory-auto.js.map +1 -0
  65. package/dist/module/lib/websocket-factory.d.ts +35 -0
  66. package/dist/module/lib/websocket-factory.d.ts.map +1 -0
  67. package/dist/module/lib/websocket-factory.js +91 -0
  68. package/dist/module/lib/websocket-factory.js.map +1 -0
  69. package/package.json +16 -6
  70. package/src/RealtimeChannel.ts +38 -12
  71. package/src/RealtimeClient.ts +45 -15
  72. package/src/index.auto.ts +14 -0
  73. package/src/index.ts +3 -0
  74. package/src/lib/constants.ts +1 -0
  75. package/src/lib/timer.ts +1 -0
  76. package/src/lib/transformers.ts +1 -1
  77. package/src/lib/version.ts +1 -1
  78. package/src/lib/websocket-factory-auto.ts +72 -0
  79. package/src/lib/websocket-factory.ts +155 -0
@@ -1,4 +1,8 @@
1
- import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants'
1
+ import {
2
+ CHANNEL_EVENTS,
3
+ CHANNEL_STATES,
4
+ MAX_PUSH_BUFFER_SIZE,
5
+ } from './lib/constants'
2
6
  import Push from './lib/push'
3
7
  import type RealtimeClient from './RealtimeClient'
4
8
  import Timer from './lib/timer'
@@ -213,8 +217,7 @@ export default class RealtimeChannel {
213
217
 
214
218
  this.presence = new RealtimePresence(this)
215
219
 
216
- this.broadcastEndpointURL =
217
- httpEndpointURL(this.socket.endPoint) + '/api/broadcast'
220
+ this.broadcastEndpointURL = httpEndpointURL(this.socket.endPoint)
218
221
  this.private = this.params.config.private || false
219
222
  }
220
223
 
@@ -571,8 +574,11 @@ export default class RealtimeChannel {
571
574
  */
572
575
  teardown() {
573
576
  this.pushBuffer.forEach((push: Push) => push.destroy())
574
- this.rejoinTimer && clearTimeout(this.rejoinTimer.timer)
577
+ this.pushBuffer = []
578
+ this.rejoinTimer.reset()
575
579
  this.joinPush.destroy()
580
+ this.state = CHANNEL_STATES.closed
581
+ this.bindings = {}
576
582
  }
577
583
 
578
584
  /** @internal */
@@ -608,13 +614,31 @@ export default class RealtimeChannel {
608
614
  if (this._canPush()) {
609
615
  pushEvent.send()
610
616
  } else {
611
- pushEvent.startTimeout()
612
- this.pushBuffer.push(pushEvent)
617
+ this._addToPushBuffer(pushEvent)
613
618
  }
614
619
 
615
620
  return pushEvent
616
621
  }
617
622
 
623
+ /** @internal */
624
+ _addToPushBuffer(pushEvent: Push) {
625
+ pushEvent.startTimeout()
626
+ this.pushBuffer.push(pushEvent)
627
+
628
+ // Enforce buffer size limit
629
+ if (this.pushBuffer.length > MAX_PUSH_BUFFER_SIZE) {
630
+ const removedPush = this.pushBuffer.shift()
631
+ if (removedPush) {
632
+ removedPush.destroy()
633
+ this.socket.log(
634
+ 'channel',
635
+ `discarded push due to buffer overflow: ${removedPush.event}`,
636
+ removedPush.payload
637
+ )
638
+ }
639
+ }
640
+ }
641
+
618
642
  /**
619
643
  * Overridable message hook
620
644
  *
@@ -757,12 +781,14 @@ export default class RealtimeChannel {
757
781
  _off(type: string, filter: { [key: string]: any }) {
758
782
  const typeLower = type.toLocaleLowerCase()
759
783
 
760
- this.bindings[typeLower] = this.bindings[typeLower].filter((bind) => {
761
- return !(
762
- bind.type?.toLocaleLowerCase() === typeLower &&
763
- RealtimeChannel.isEqual(bind.filter, filter)
764
- )
765
- })
784
+ if (this.bindings[typeLower]) {
785
+ this.bindings[typeLower] = this.bindings[typeLower].filter((bind) => {
786
+ return !(
787
+ bind.type?.toLocaleLowerCase() === typeLower &&
788
+ RealtimeChannel.isEqual(bind.filter, filter)
789
+ )
790
+ })
791
+ }
766
792
  return this
767
793
  }
768
794
 
@@ -1,4 +1,4 @@
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
@@ -199,15 +197,37 @@ export default class RealtimeClient {
199
197
  this._setAuthSafely('connect')
200
198
 
201
199
  // Establish WebSocket connection
202
- if (!this.transport) {
203
- this.transport = WebSocket
204
- }
205
- if (!this.transport) {
206
- this._setConnectionState('disconnected')
207
- throw new Error('No transport provided')
200
+ if (this.transport) {
201
+ // Use custom transport if provided
202
+ this.conn = new this.transport(this.endpointURL()) as WebSocketLike
203
+ } else {
204
+ // Try to use native WebSocket
205
+ try {
206
+ this.conn = WebSocketFactory.createWebSocket(this.endpointURL())
207
+ } catch (error) {
208
+ this._setConnectionState('disconnected')
209
+ const errorMessage = (error as Error).message
210
+
211
+ // Provide helpful error message based on environment
212
+ if (errorMessage.includes('Node.js')) {
213
+ throw new Error(
214
+ `${errorMessage}\n\n` +
215
+ 'To use Realtime in Node.js, you need to provide a WebSocket implementation:\n\n' +
216
+ 'Option 1: Use Node.js 22+ which has native WebSocket support\n' +
217
+ 'Option 2: Install and provide the "ws" package:\n\n' +
218
+ ' npm install ws\n\n' +
219
+ ' import ws from "ws"\n' +
220
+ ' const client = new RealtimeClient(url, {\n' +
221
+ ' ...options,\n' +
222
+ ' transport: ws\n' +
223
+ ' })' +
224
+ 'Option 3: Use the auto export which handles WebSocket automatically:\n' +
225
+ ' import { RealtimeClient } from "@supabase/realtime-js/auto"'
226
+ )
227
+ }
228
+ throw new Error(`WebSocket not available: ${errorMessage}`)
229
+ }
208
230
  }
209
-
210
- this.conn = new this.transport!(this.endpointURL()) as WebSocketLike
211
231
  this._setupConnectionHandlers()
212
232
  }
213
233
 
@@ -462,10 +482,16 @@ export default class RealtimeClient {
462
482
  if (customFetch) {
463
483
  _fetch = customFetch
464
484
  } else if (typeof fetch === 'undefined') {
485
+ // Node.js environment without native fetch
465
486
  _fetch = (...args) =>
466
- import('@supabase/node-fetch' as any).then(({ default: fetch }) =>
467
- fetch(...args)
468
- )
487
+ import('@supabase/node-fetch' as any)
488
+ .then(({ default: fetch }) => fetch(...args))
489
+ .catch((error) => {
490
+ throw new Error(
491
+ `Failed to load @supabase/node-fetch: ${error.message}. ` +
492
+ `This is required for HTTP requests in Node.js environments without native fetch.`
493
+ )
494
+ })
469
495
  } else {
470
496
  _fetch = fetch
471
497
  }
@@ -577,7 +603,11 @@ export default class RealtimeClient {
577
603
  private _setupConnectionHandlers(): void {
578
604
  if (!this.conn) return
579
605
 
580
- this.conn.binaryType = 'arraybuffer'
606
+ // Set binary type if supported (browsers and most WebSocket implementations)
607
+ if ('binaryType' in this.conn) {
608
+ ;(this.conn as any).binaryType = 'arraybuffer'
609
+ }
610
+
581
611
  this.conn.onopen = () => this._onConnOpen()
582
612
  this.conn.onerror = (error: Event) => this._onConnError(error)
583
613
  this.conn.onmessage = (event: any) => this._onConnMessage(event)
@@ -0,0 +1,14 @@
1
+ // TODO(@mandarini): Remove this file in v3.0.0 - also update package.json exports
2
+ /**
3
+ * @deprecated This auto-detection export will be removed in v3.0.0
4
+ * Use the main export with explicit transport instead:
5
+ *
6
+ * import { RealtimeClient } from "@supabase/realtime-js"
7
+ * import ws from "ws"
8
+ * const client = new RealtimeClient(url, { transport: ws })
9
+ */
10
+
11
+ export * from './index'
12
+
13
+ // Override WebSocket factory with auto-detection version
14
+ export { default as WebSocketFactory } from './lib/websocket-factory-auto'
package/src/index.ts CHANGED
@@ -22,6 +22,7 @@ import RealtimePresence, {
22
22
  RealtimePresenceLeavePayload,
23
23
  REALTIME_PRESENCE_LISTEN_EVENTS,
24
24
  } from './RealtimePresence'
25
+ import WebSocketFactory, { WebSocketLike } from './lib/websocket-factory'
25
26
 
26
27
  export {
27
28
  RealtimePresence,
@@ -45,4 +46,6 @@ export {
45
46
  REALTIME_PRESENCE_LISTEN_EVENTS,
46
47
  REALTIME_SUBSCRIBE_STATES,
47
48
  REALTIME_CHANNEL_STATES,
49
+ WebSocketFactory,
50
+ WebSocketLike,
48
51
  }
@@ -8,6 +8,7 @@ export const VERSION = version
8
8
  export const DEFAULT_TIMEOUT = 10000
9
9
 
10
10
  export const WS_CLOSE_NORMAL = 1000
11
+ export const MAX_PUSH_BUFFER_SIZE = 100
11
12
 
12
13
  export enum SOCKET_STATES {
13
14
  connecting = 0,
package/src/lib/timer.ts CHANGED
@@ -22,6 +22,7 @@ export default class Timer {
22
22
  reset() {
23
23
  this.tries = 0
24
24
  clearTimeout(this.timer)
25
+ this.timer = undefined
25
26
  }
26
27
 
27
28
  // Cancels any previous scheduleTimeout and schedules callback
@@ -250,5 +250,5 @@ export const httpEndpointURL = (socketUrl: string): string => {
250
250
  let url = socketUrl
251
251
  url = url.replace(/^ws/i, 'http')
252
252
  url = url.replace(/(\/socket\/websocket|\/socket|\/websocket)\/?$/i, '')
253
- return url.replace(/\/+$/, '')
253
+ return url.replace(/\/+$/, '') + '/api/broadcast'
254
254
  }
@@ -1 +1 @@
1
- export const version = '2.13.0'
1
+ export const version = '2.15.0-next.1'
@@ -0,0 +1,72 @@
1
+ // TODO(@mandarini): Remove this file in v3.0.0 - also update package.json exports
2
+
3
+ import { WebSocketFactory } from './websocket-factory'
4
+
5
+ /**
6
+ * WebSocketFactoryAuto extends WebSocketFactory with automatic WebSocket detection.
7
+ *
8
+ * @deprecated This class will be removed in v3.0.0. The main export will require
9
+ * explicit WebSocket transport for Node.js < 22 environments.
10
+ */
11
+ export class WebSocketFactoryAuto extends WebSocketFactory {
12
+ // Static flag to track if warning has been shown
13
+ private static hasShownDeprecationWarning = false
14
+
15
+ /**
16
+ * Dynamic require for 'ws' package
17
+ * @private
18
+ */
19
+ private static dynamicRequire(moduleId: string): any {
20
+ try {
21
+ if (typeof require !== 'undefined') {
22
+ return require(moduleId)
23
+ }
24
+ return null
25
+ } catch {
26
+ return null
27
+ }
28
+ }
29
+
30
+ /**
31
+ * Show deprecation warning once per process
32
+ * @private
33
+ */
34
+ private static showDeprecationWarning() {
35
+ if (!this.hasShownDeprecationWarning) {
36
+ this.hasShownDeprecationWarning = true
37
+ console.warn(
38
+ '[DEPRECATED] @supabase/realtime-js/auto will be removed in v3.0.0. ' +
39
+ 'Use explicit transport instead: ' +
40
+ 'https://supabase.com/docs/guides/realtime/js-client#nodejs-support'
41
+ )
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Override getWebSocketConstructor to add dynamic 'ws' loading for Node.js
47
+ */
48
+ public static getWebSocketConstructor(): typeof WebSocket {
49
+ try {
50
+ // First try the base class (handles browser, Node.js 22+, edge cases, etc.)
51
+ return super.getWebSocketConstructor()
52
+ } catch (error) {
53
+ // If base class fails and we're in Node.js, try dynamic 'ws' import
54
+ if (
55
+ typeof process !== 'undefined' &&
56
+ process.versions &&
57
+ process.versions.node
58
+ ) {
59
+ try {
60
+ const ws = this.dynamicRequire('ws')
61
+ if (ws) {
62
+ return ws.WebSocket ?? ws
63
+ }
64
+ } catch {}
65
+ }
66
+ // Re-throw original error if dynamic import fails
67
+ throw error
68
+ }
69
+ }
70
+ }
71
+
72
+ export default WebSocketFactoryAuto
@@ -0,0 +1,155 @@
1
+ export interface WebSocketLike {
2
+ readonly CONNECTING: number
3
+ readonly OPEN: number
4
+ readonly CLOSING: number
5
+ readonly CLOSED: number
6
+ readonly readyState: number
7
+ readonly url: string
8
+ readonly protocol: string
9
+
10
+ close(code?: number, reason?: string): void
11
+ send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void
12
+
13
+ onopen: ((this: any, ev: Event) => any) | null
14
+ onmessage: ((this: any, ev: MessageEvent) => any) | null
15
+ onclose: ((this: any, ev: CloseEvent) => any) | null
16
+ onerror: ((this: any, ev: Event) => any) | null
17
+
18
+ addEventListener(type: string, listener: EventListener): void
19
+ removeEventListener(type: string, listener: EventListener): void
20
+
21
+ // Add additional properties that may exist on WebSocket implementations
22
+ binaryType?: string
23
+ bufferedAmount?: number
24
+ extensions?: string
25
+ dispatchEvent?: (event: Event) => boolean
26
+ }
27
+
28
+ export interface WebSocketEnvironment {
29
+ type: 'native' | 'ws' | 'cloudflare' | 'unsupported'
30
+ constructor?: any
31
+ error?: string
32
+ workaround?: string
33
+ }
34
+
35
+ export class WebSocketFactory {
36
+ private static detectEnvironment(): WebSocketEnvironment {
37
+ if (typeof WebSocket !== 'undefined') {
38
+ return { type: 'native', constructor: WebSocket }
39
+ }
40
+
41
+ if (
42
+ typeof globalThis !== 'undefined' &&
43
+ typeof (globalThis as any).WebSocket !== 'undefined'
44
+ ) {
45
+ return { type: 'native', constructor: (globalThis as any).WebSocket }
46
+ }
47
+
48
+ if (
49
+ typeof global !== 'undefined' &&
50
+ typeof (global as any).WebSocket !== 'undefined'
51
+ ) {
52
+ return { type: 'native', constructor: (global as any).WebSocket }
53
+ }
54
+
55
+ if (
56
+ typeof globalThis !== 'undefined' &&
57
+ typeof (globalThis as any).WebSocketPair !== 'undefined' &&
58
+ typeof globalThis.WebSocket === 'undefined'
59
+ ) {
60
+ return {
61
+ type: 'cloudflare',
62
+ error:
63
+ 'Cloudflare Workers detected. WebSocket clients are not supported in Cloudflare Workers.',
64
+ workaround:
65
+ 'Use Cloudflare Workers WebSocket API for server-side WebSocket handling, or deploy to a different runtime.',
66
+ }
67
+ }
68
+
69
+ if (
70
+ (typeof globalThis !== 'undefined' && (globalThis as any).EdgeRuntime) ||
71
+ (typeof navigator !== 'undefined' &&
72
+ navigator.userAgent?.includes('Vercel-Edge'))
73
+ ) {
74
+ return {
75
+ type: 'unsupported',
76
+ error:
77
+ 'Edge runtime detected (Vercel Edge/Netlify Edge). WebSockets are not supported in edge functions.',
78
+ workaround:
79
+ 'Use serverless functions or a different deployment target for WebSocket functionality.',
80
+ }
81
+ }
82
+
83
+ if (
84
+ typeof process !== 'undefined' &&
85
+ process.versions &&
86
+ process.versions.node
87
+ ) {
88
+ const nodeVersion = parseInt(process.versions.node.split('.')[0])
89
+
90
+ // Node.js 22+ should have native WebSocket
91
+ if (nodeVersion >= 22) {
92
+ // Check if native WebSocket is available (should be in Node.js 22+)
93
+ if (typeof globalThis.WebSocket !== 'undefined') {
94
+ return { type: 'native', constructor: globalThis.WebSocket }
95
+ }
96
+ // If not available, user needs to provide it
97
+ return {
98
+ type: 'unsupported',
99
+ error: `Node.js ${nodeVersion} detected but native WebSocket not found.`,
100
+ workaround:
101
+ 'Provide a WebSocket implementation via the transport option.',
102
+ }
103
+ }
104
+
105
+ // Node.js < 22 doesn't have native WebSocket
106
+ return {
107
+ type: 'unsupported',
108
+ error: `Node.js ${nodeVersion} detected without native WebSocket support.`,
109
+ workaround:
110
+ 'For Node.js < 22, install "ws" package and provide it via the transport option:\n' +
111
+ 'import ws from "ws"\n' +
112
+ 'new RealtimeClient(url, { transport: ws })',
113
+ }
114
+ }
115
+
116
+ return {
117
+ type: 'unsupported',
118
+ error: 'Unknown JavaScript runtime without WebSocket support.',
119
+ workaround:
120
+ "Ensure you're running in a supported environment (browser, Node.js, Deno) or provide a custom WebSocket implementation.",
121
+ }
122
+ }
123
+
124
+ public static getWebSocketConstructor(): typeof WebSocket {
125
+ const env = this.detectEnvironment()
126
+ if (env.constructor) {
127
+ return env.constructor
128
+ }
129
+ let errorMessage =
130
+ env.error || 'WebSocket not supported in this environment.'
131
+ if (env.workaround) {
132
+ errorMessage += `\n\nSuggested solution: ${env.workaround}`
133
+ }
134
+ throw new Error(errorMessage)
135
+ }
136
+
137
+ public static createWebSocket(
138
+ url: string | URL,
139
+ protocols?: string | string[]
140
+ ): WebSocketLike {
141
+ const WS = this.getWebSocketConstructor()
142
+ return new WS(url, protocols)
143
+ }
144
+
145
+ public static isWebSocketSupported(): boolean {
146
+ try {
147
+ const env = this.detectEnvironment()
148
+ return env.type === 'native' || env.type === 'ws'
149
+ } catch {
150
+ return false
151
+ }
152
+ }
153
+ }
154
+
155
+ export default WebSocketFactory