Skip to content

Commit a1ac269

Browse files
committed
feat: add Realtime Channel helper methods
1 parent 501b1aa commit a1ac269

File tree

4 files changed

+78
-31
lines changed

4 files changed

+78
-31
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.23.0-next.8",
4242
"@supabase/postgrest-js": "^1.0.0-next.2",
43-
"@supabase/realtime-js": "^1.7.3",
43+
"@supabase/realtime-js": "^1.8.0-next.13",
4444
"@supabase/storage-js": "^1.8.0-next.1",
4545
"cross-fetch": "^3.1.5"
4646
},

src/SupabaseClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { DEFAULT_HEADERS } from './lib/constants'
1111
import { fetchWithAuth } from './lib/fetch'
1212
import { stripTrailingSlash } from './lib/helpers'
1313
import { SupabaseAuthClient } from './lib/SupabaseAuthClient'
14-
import { SupabaseRealtimeClient } from './lib/SupabaseRealtimeClient'
14+
import { SupabaseRealtimeChannel } from './lib/SupabaseRealtimeChannel'
1515
import { Fetch, GenericSchema, SupabaseClientOptions, SupabaseAuthClientOptions } from './lib/types'
1616

1717
const DEFAULT_OPTIONS = {
@@ -180,12 +180,12 @@ export default class SupabaseClient<
180180
/**
181181
* Creates a channel with Broadcast and Presence.
182182
*/
183-
channel(name: string, opts?: { [key: string]: any }): SupabaseRealtimeClient {
183+
channel(name: string, opts?: { [key: string]: any }): SupabaseRealtimeChannel {
184184
if (!this.realtime.isConnected()) {
185185
this.realtime.connect()
186186
}
187187

188-
return new SupabaseRealtimeClient(this.realtime, name, opts)
188+
return new SupabaseRealtimeChannel(name, opts, this.realtime)
189189
}
190190

191191
/**

src/lib/SupabaseRealtimeClient.ts renamed to src/lib/SupabaseRealtimeChannel.ts

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
11
import { RealtimeChannel, RealtimeClient, Transformers } from '@supabase/realtime-js'
2-
import { SupabaseRealtimePayload } from './types'
32

4-
export class SupabaseRealtimeClient {
3+
export class SupabaseRealtimeChannel {
54
socket: RealtimeClient
65
channel: RealtimeChannel
76

8-
constructor(socket: RealtimeClient, name: string, opts: { [key: string]: any } = {}) {
7+
constructor(name: string, opts: { [key: string]: any } = {}, socket: RealtimeClient) {
98
this.socket = socket
109
this.channel = socket.channel(`realtime:${name}`, opts) as RealtimeChannel
1110
}
1211

13-
private getPayloadRecords(payload: any) {
14-
const records = {
15-
new: {},
16-
old: {},
17-
}
18-
19-
if (payload.type === 'INSERT' || payload.type === 'UPDATE') {
20-
records.new = Transformers.convertChangeData(payload.columns, payload.record)
21-
}
22-
23-
if (payload.type === 'UPDATE' || payload.type === 'DELETE') {
24-
records.old = Transformers.convertChangeData(payload.columns, payload.old_record)
25-
}
26-
27-
return records
28-
}
29-
3012
/**
3113
* The event you want to listen to.
3214
*
@@ -88,4 +70,69 @@ export class SupabaseRealtimeClient {
8870

8971
return this.channel
9072
}
73+
74+
presenceList() {
75+
return this.channel.presence.list()
76+
}
77+
78+
send(
79+
payload: { type: string; [key: string]: any },
80+
opts: { [key: string]: any } = {}
81+
): Promise<['ok' | 'timeout', number]> {
82+
return new Promise((resolve) => {
83+
const now = performance.now()
84+
const timeout = opts.timeout || this.channel.timeout
85+
86+
this.channel
87+
.push(payload.type, payload, timeout)
88+
.receive('ok', () => {
89+
const diff = performance.now() - now
90+
resolve(['ok', diff])
91+
})
92+
.receive('timeout', () => {
93+
resolve(['timeout', timeout])
94+
})
95+
})
96+
}
97+
98+
async track(
99+
payload: { [key: string]: any },
100+
opts: { [key: string]: any } = {}
101+
): Promise<['ok' | 'timeout', number]> {
102+
return await this.send(
103+
{
104+
type: 'presence',
105+
event: 'track',
106+
payload,
107+
},
108+
opts
109+
)
110+
}
111+
112+
async untrack(opts: { [key: string]: any } = {}): Promise<['ok' | 'timeout', number]> {
113+
return await this.send(
114+
{
115+
type: 'presence',
116+
event: 'untrack',
117+
},
118+
opts
119+
)
120+
}
121+
122+
private getPayloadRecords(payload: any) {
123+
const records = {
124+
new: {},
125+
old: {},
126+
}
127+
128+
if (payload.type === 'INSERT' || payload.type === 'UPDATE') {
129+
records.new = Transformers.convertChangeData(payload.columns, payload.record)
130+
}
131+
132+
if (payload.type === 'UPDATE' || payload.type === 'DELETE') {
133+
records.old = Transformers.convertChangeData(payload.columns, payload.old_record)
134+
}
135+
136+
return records
137+
}
91138
}

0 commit comments

Comments
 (0)