Skip to content

Commit bd20583

Browse files
authored
Merge pull request #328 from supabase/fix/realtime-subscription
fix: create subscription only when .on method is called
2 parents abec733 + 6cf54a2 commit bd20583

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/SupabaseClient.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ export default class SupabaseClient {
131131
}
132132

133133
/**
134-
* Remove all active subscriptions.
134+
* Remove all subscriptions.
135135
*/
136-
removeAllSubscriptions() {
137-
this.realtime.channels.forEach((sub) => {
138-
this.removeSubscription(sub)
139-
})
136+
async removeAllSubscriptions() {
137+
const subscriptions = this.realtime.channels.slice()
138+
return await Promise.allSettled(subscriptions.map((sub) => this.removeSubscription(sub)))
140139
}
141140

142141
/**
@@ -149,12 +148,14 @@ export default class SupabaseClient {
149148
try {
150149
await this._closeSubscription(subscription)
151150

152-
const openSubscriptions = this.getSubscriptions().length
153-
if (!openSubscriptions) {
151+
const allSubscriptions = this.getSubscriptions()
152+
const openSubscriptionsCount = allSubscriptions.filter((chan) => chan.isJoined()).length
153+
154+
if (!allSubscriptions.length) {
154155
const { error } = await this.realtime.disconnect()
155156
if (error) return resolve({ error })
156157
}
157-
return resolve({ error: null, data: { openSubscriptions } })
158+
return resolve({ error: null, data: { openSubscriptions: openSubscriptionsCount } })
158159
} catch (error) {
159160
return resolve({ error })
160161
}
@@ -165,6 +166,11 @@ export default class SupabaseClient {
165166
if (!subscription.isClosed()) {
166167
await this._closeChannel(subscription)
167168
}
169+
170+
return new Promise((resolve) => {
171+
this.realtime.remove(subscription)
172+
return resolve(true)
173+
})
168174
}
169175

170176
/**
@@ -225,7 +231,6 @@ export default class SupabaseClient {
225231
subscription
226232
.unsubscribe()
227233
.receive('ok', () => {
228-
this.realtime.remove(subscription)
229234
return resolve(true)
230235
})
231236
.receive('error', (e: Error) => reject(e))

src/lib/SupabaseQueryBuilder.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import { RealtimeClient } from '@supabase/realtime-js'
44
import { Fetch, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
55

66
export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
7-
private _subscription: SupabaseRealtimeClient
7+
private _subscription: SupabaseRealtimeClient | null = null
88
private _realtime: RealtimeClient
9+
private _headers: { [key: string]: string }
10+
private _schema: string
11+
private _table: string
912

1013
constructor(
1114
url: string,
@@ -25,12 +28,14 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
2528
) {
2629
super(url, { headers, schema, fetch })
2730

28-
this._subscription = new SupabaseRealtimeClient(realtime, headers, schema, table)
2931
this._realtime = realtime
32+
this._headers = headers
33+
this._schema = schema
34+
this._table = table
3035
}
3136

3237
/**
33-
* Subscribe to realtime changes in your databse.
38+
* Subscribe to realtime changes in your database.
3439
* @param event The database event which you would like to receive updates for, or you can use the special wildcard `*` to listen to all changes.
3540
* @param callback A callback that will handle the payload that is sent whenever your database changes.
3641
*/
@@ -41,6 +46,14 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
4146
if (!this._realtime.isConnected()) {
4247
this._realtime.connect()
4348
}
49+
if (!this._subscription) {
50+
this._subscription = new SupabaseRealtimeClient(
51+
this._realtime,
52+
this._headers,
53+
this._schema,
54+
this._table
55+
)
56+
}
4457
return this._subscription.on(event, callback)
4558
}
4659
}

0 commit comments

Comments
 (0)