Skip to content

Commit c5f6b5f

Browse files
committed
feat: add channel to SupabaseClient
1 parent 09065a6 commit c5f6b5f

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"@supabase/functions-js": "^1.3.3",
4141
"@supabase/gotrue-js": "^1.22.12",
4242
"@supabase/postgrest-js": "^0.37.2",
43-
"@supabase/realtime-js": "^1.6.2",
43+
"@supabase/realtime-js": "^1.7.0",
4444
"@supabase/storage-js": "^1.7.0"
4545
},
4646
"devDependencies": {

src/SupabaseClient.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import { SupabaseStorageClient } from '@supabase/storage-js'
77
import { FunctionsClient } from '@supabase/functions-js'
88
import { PostgrestClient } from '@supabase/postgrest-js'
99
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'
1116

1217
const DEFAULT_OPTIONS = {
1318
schema: 'public',
@@ -155,6 +160,20 @@ export default class SupabaseClient {
155160
return rest.rpc<T>(fn, params, { head, count })
156161
}
157162

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+
158177
/**
159178
* Closes and removes all subscriptions and returns a list of removed
160179
* subscriptions and their errors.
@@ -174,6 +193,23 @@ export default class SupabaseClient {
174193
})
175194
}
176195

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+
177213
/**
178214
* Closes and removes a subscription and returns the number of open subscriptions.
179215
*
@@ -192,7 +228,7 @@ export default class SupabaseClient {
192228
}
193229

194230
private async _closeSubscription(
195-
subscription: RealtimeSubscription
231+
subscription: RealtimeSubscription | RealtimeChannel
196232
): Promise<{ error: Error | null }> {
197233
let error = null
198234

@@ -207,7 +243,7 @@ export default class SupabaseClient {
207243
}
208244

209245
private _unsubscribeSubscription(
210-
subscription: RealtimeSubscription
246+
subscription: RealtimeSubscription | RealtimeChannel
211247
): Promise<{ error: Error | null }> {
212248
return new Promise((resolve) => {
213249
subscription

0 commit comments

Comments
 (0)