@supabase/realtime-js 2.112.4 → 2.113.0-canary.0

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.
@@ -1,4 +1,9 @@
1
- import { CHANNEL_EVENTS, CHANNEL_STATES } from './lib/constants'
1
+ import {
2
+ CHANNEL_EVENTS,
3
+ CHANNEL_STATES,
4
+ DEFAULT_POSTGRES_CHANGES_WAIT_TIMEOUT,
5
+ POSTGRES_CHANGES_WAIT_ERROR_GRACE,
6
+ } from './lib/constants'
2
7
  import type { ChannelState } from './lib/constants'
3
8
  import type RealtimeClient from './RealtimeClient'
4
9
  import RealtimePresence, { REALTIME_PRESENCE_LISTEN_EVENTS } from './RealtimePresence'
@@ -65,6 +70,34 @@ export type RealtimeChannelOptions = {
65
70
  * defines if the channel is private or not and if RLS policies will be used to check data
66
71
  */
67
72
  private?: boolean
73
+ /**
74
+ * By default, `subscribe()` reports `SUBSCRIBED` as soon as the channel itself has joined,
75
+ * which can happen before the server has actually established the `postgres_changes`
76
+ * subscription (e.g. before the replication slot is streaming). Set `wait: true` to instead
77
+ * hold the `SUBSCRIBED` callback until the server confirms the postgres_changes subscription
78
+ * is active.
79
+ *
80
+ * If the subscription cannot be established, the server rejects the join and `subscribe()`
81
+ * reports `CHANNEL_ERROR` with the server's reason (e.g.
82
+ * `PostgresChangesSubscribeTimeout: ...` when the wait ran out, or
83
+ * `RealtimeDisabledForConfiguration: ...` when the table is not enabled for Realtime).
84
+ *
85
+ * Has no effect on a channel with no `postgres_changes` bindings.
86
+ *
87
+ * Setting `wait: true` automatically extends the channel's join timeout so it outlasts the
88
+ * server's held reply, so you don't need to pass a larger `timeout` to `subscribe()` yourself.
89
+ * As a side effect of how Phoenix tracks the join timeout, other pushes on the channel that
90
+ * do not pass an explicit timeout inherit the extended value too.
91
+ */
92
+ postgres_changes_options?: {
93
+ wait?: boolean
94
+ /**
95
+ * Milliseconds the server should wait for postgres_changes subscription confirmation when
96
+ * `wait` is `true`. Defaults to 15000, and the server clamps it to its own configured
97
+ * maximum (20s by default), so asking for more waits less.
98
+ */
99
+ timeout?: number
100
+ }
68
101
  }
69
102
  }
70
103
 
@@ -395,7 +428,7 @@ export default class RealtimeChannel {
395
428
  }
396
429
  if (this.channelAdapter.isClosed()) {
397
430
  const {
398
- config: { broadcast, presence, private: isPrivate },
431
+ config: { broadcast, presence, private: isPrivate, postgres_changes_options },
399
432
  } = this.params
400
433
 
401
434
  const postgres_changes = this.bindings.postgres_changes?.map((r) => r.filter) ?? []
@@ -410,6 +443,7 @@ export default class RealtimeChannel {
410
443
  presence: { ...presence, enabled: presence_enabled },
411
444
  postgres_changes,
412
445
  private: isPrivate,
446
+ ...(postgres_changes_options ? { postgres_changes_options } : {}),
413
447
  }
414
448
 
415
449
  if (this.socket.accessTokenValue) {
@@ -426,8 +460,17 @@ export default class RealtimeChannel {
426
460
 
427
461
  this._updateFilterMessage()
428
462
 
463
+ const joinTimeout =
464
+ postgres_changes_options?.wait && postgres_changes.length > 0
465
+ ? Math.max(
466
+ timeout,
467
+ (postgres_changes_options.timeout ?? DEFAULT_POSTGRES_CHANGES_WAIT_TIMEOUT) +
468
+ POSTGRES_CHANGES_WAIT_ERROR_GRACE
469
+ )
470
+ : timeout
471
+
429
472
  this.channelAdapter
430
- .subscribe(timeout)
473
+ .subscribe(joinTimeout)
431
474
  .receive('ok', async ({ postgres_changes }: PostgresChangesFilters) => {
432
475
  // Only refresh auth if using callback-based tokens
433
476
  if (!this.socket._isManualToken()) {
@@ -19,6 +19,15 @@ export const VERSION = version
19
19
 
20
20
  export const DEFAULT_TIMEOUT = 10000
21
21
 
22
+ /** Mirrors the Realtime server's own default for `postgres_changes_options.timeout`. */
23
+ export const DEFAULT_POSTGRES_CHANGES_WAIT_TIMEOUT = 15000
24
+
25
+ /**
26
+ * Headroom added to the join timeout when waiting on postgres_changes, covering the
27
+ * CHANNEL_ERROR_BACKOFF_MS sleep (5s by default) the server takes before rejecting a join.
28
+ */
29
+ export const POSTGRES_CHANGES_WAIT_ERROR_GRACE = 10000
30
+
22
31
  export const WS_CLOSE_NORMAL = 1000
23
32
  export const MAX_PUSH_BUFFER_SIZE = 100
24
33
 
@@ -4,4 +4,4 @@
4
4
  // - Debugging and support (identifying which version is running)
5
5
  // - Telemetry and logging (version reporting in errors/analytics)
6
6
  // - Ensuring build artifacts match the published package version
7
- export const version = '2.112.4'
7
+ export const version = '2.113.0-canary.0'