Skip to content

Commit c982288

Browse files
committed
fix: make realtime work with gotrue v2
1 parent 2fdd6fa commit c982288

File tree

3 files changed

+26
-22
lines changed

3 files changed

+26
-22
lines changed

src/SupabaseClient.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,11 @@ export default class SupabaseClient {
167167
* Activated when vsndate query param is present in the WebSocket URL.
168168
*/
169169
channel(name: string, opts: { selfBroadcast: boolean; [key: string]: any }): RealtimeChannel {
170-
const userToken = this.auth.session()?.access_token ?? this.supabaseKey
171-
172170
if (!this.realtime.isConnected()) {
173171
this.realtime.connect()
174172
}
175173

176-
return this.realtime.channel(name, { ...opts, user_token: userToken }) as RealtimeChannel
174+
return this.realtime.channel(name, opts) as RealtimeChannel
177175
}
178176

179177
/**

src/lib/SupabaseQueryBuilder.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Fetch, GenericObject, SupabaseEventTypes, SupabaseRealtimePayload } fro
66
export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
77
private _subscription: SupabaseRealtimeClient | null = null
88
private _realtime: RealtimeClient
9-
private _headers: GenericObject
109
private _schema: string
1110
private _table: string
1211

@@ -31,7 +30,6 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
3130
super(url, { headers, schema, fetch, shouldThrowOnError })
3231

3332
this._realtime = realtime
34-
this._headers = headers
3533
this._schema = schema
3634
this._table = table
3735
}
@@ -49,12 +47,7 @@ export class SupabaseQueryBuilder<T> extends PostgrestQueryBuilder<T> {
4947
this._realtime.connect()
5048
}
5149
if (!this._subscription) {
52-
this._subscription = new SupabaseRealtimeClient(
53-
this._realtime,
54-
this._headers,
55-
this._schema,
56-
this._table
57-
)
50+
this._subscription = new SupabaseRealtimeClient(this._realtime, this._schema, this._table)
5851
}
5952
return this._subscription.on(event, callback)
6053
}

src/lib/SupabaseRealtimeClient.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import { RealtimeSubscription, RealtimeClient, Transformers } from '@supabase/realtime-js'
2-
import { GenericObject, SupabaseEventTypes, SupabaseRealtimePayload } from './types'
1+
import { RealtimeClient, RealtimeSubscription, Transformers } from '@supabase/realtime-js'
2+
import { SupabaseEventTypes, SupabaseRealtimePayload } from './types'
33

44
export class SupabaseRealtimeClient {
5+
socket: RealtimeClient
56
subscription: RealtimeSubscription
67

7-
constructor(socket: RealtimeClient, headers: GenericObject, schema: string, tableName: string) {
8-
const chanParams: GenericObject = {}
8+
constructor(socket: RealtimeClient, schema: string, tableName: string) {
99
const topic = tableName === '*' ? `realtime:${schema}` : `realtime:${schema}:${tableName}`
10-
const userToken = headers['Authorization'].split(' ')[1]
1110

12-
if (userToken) {
13-
chanParams['user_token'] = userToken
14-
}
15-
16-
this.subscription = socket.channel(topic, chanParams) as RealtimeSubscription
11+
this.socket = socket
12+
this.subscription = socket.channel(topic) as RealtimeSubscription
1713
}
1814

1915
private getPayloadRecords(payload: any) {
@@ -62,13 +58,30 @@ export class SupabaseRealtimeClient {
6258
* Enables the subscription.
6359
*/
6460
subscribe(callback: Function = () => {}) {
61+
// if the socket already has a good accessToken
62+
// we can just use it strait away∏
63+
if (this.socket.accessToken) {
64+
this.subscription.updateJoinPayload({
65+
user_token: this.socket.accessToken,
66+
})
67+
}
68+
6569
this.subscription.onError((e: Error) => callback('SUBSCRIPTION_ERROR', e))
6670
this.subscription.onClose(() => callback('CLOSED'))
6771
this.subscription
6872
.subscribe()
69-
.receive('ok', () => callback('SUBSCRIBED'))
73+
.receive('ok', () => {
74+
callback('SUBSCRIBED')
75+
76+
// re-set the accessToken again in case it was set while
77+
// the subscription was isJoining
78+
if (this.socket.accessToken) {
79+
this.socket.setAuth(this.socket.accessToken)
80+
}
81+
})
7082
.receive('error', (e: Error) => callback('SUBSCRIPTION_ERROR', e))
7183
.receive('timeout', () => callback('RETRYING_AFTER_TIMEOUT'))
84+
7285
return this.subscription
7386
}
7487
}

0 commit comments

Comments
 (0)