@@ -7,7 +7,12 @@ import { SupabaseStorageClient } from '@supabase/storage-js'
7
7
import { FunctionsClient } from '@supabase/functions-js'
8
8
import { PostgrestClient } from '@supabase/postgrest-js'
9
9
import { AuthChangeEvent } from '@supabase/gotrue-js'
10
- import { RealtimeClient , RealtimeSubscription , RealtimeClientOptions } from '@supabase/realtime-js'
10
+ import {
11
+ RealtimeClient ,
12
+ RealtimeSubscription ,
13
+ RealtimeClientOptions ,
14
+ RealtimeChannel ,
15
+ } from '@supabase/realtime-js'
11
16
12
17
const DEFAULT_OPTIONS = {
13
18
schema : 'public' ,
@@ -155,6 +160,20 @@ export default class SupabaseClient {
155
160
return rest . rpc < T > ( fn , params , { head, count } )
156
161
}
157
162
163
+ /**
164
+ * Creates a channel with Broadcast and Presence.
165
+ * Activated when vsndate query param is present in the WebSocket URL.
166
+ */
167
+ channel ( name : string , opts : { selfBroadcast : boolean ; [ key : string ] : any } ) : RealtimeChannel {
168
+ const userToken = this . auth . session ( ) ?. access_token ?? this . supabaseKey
169
+
170
+ if ( ! this . realtime . isConnected ( ) ) {
171
+ this . realtime . connect ( )
172
+ }
173
+
174
+ return this . realtime . channel ( name , { ...opts , user_token : userToken } ) as RealtimeChannel
175
+ }
176
+
158
177
/**
159
178
* Closes and removes all subscriptions and returns a list of removed
160
179
* subscriptions and their errors.
@@ -174,6 +193,23 @@ export default class SupabaseClient {
174
193
} )
175
194
}
176
195
196
+ /**
197
+ * Closes and removes a channel and returns the number of open channels.
198
+ *
199
+ * @param channel The channel you want to close and remove.
200
+ */
201
+ async removeChannel (
202
+ channel : RealtimeChannel
203
+ ) : Promise < { data : { openChannels : number } ; error : Error | null } > {
204
+ const { error } = await this . _closeSubscription ( channel )
205
+ const allChans : RealtimeSubscription [ ] = this . getSubscriptions ( )
206
+ const openChanCount = allChans . filter ( ( chan ) => chan . isJoined ( ) ) . length
207
+
208
+ if ( allChans . length === 0 ) await this . realtime . disconnect ( )
209
+
210
+ return { data : { openChannels : openChanCount } , error }
211
+ }
212
+
177
213
/**
178
214
* Closes and removes a subscription and returns the number of open subscriptions.
179
215
*
@@ -192,7 +228,7 @@ export default class SupabaseClient {
192
228
}
193
229
194
230
private async _closeSubscription (
195
- subscription : RealtimeSubscription
231
+ subscription : RealtimeSubscription | RealtimeChannel
196
232
) : Promise < { error : Error | null } > {
197
233
let error = null
198
234
@@ -207,7 +243,7 @@ export default class SupabaseClient {
207
243
}
208
244
209
245
private _unsubscribeSubscription (
210
- subscription : RealtimeSubscription
246
+ subscription : RealtimeSubscription | RealtimeChannel
211
247
) : Promise < { error : Error | null } > {
212
248
return new Promise ( ( resolve ) => {
213
249
subscription
0 commit comments