Skip to content

Commit 613b990

Browse files
authored
feat: Presence enabled flag on join payload (#496)
Changes the join payload to include a flag to enable/disable presence features. The flag is automatically set if we find a callback for any event of type `presence`.
1 parent 7123690 commit 613b990

File tree

4 files changed

+167
-68
lines changed

4 files changed

+167
-68
lines changed

src/RealtimeChannel.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export type RealtimeChannelOptions = {
2323
/**
2424
* key option is used to track presence payload across clients
2525
*/
26-
presence?: { key?: string }
26+
presence?: { key?: string; enabled?: boolean }
2727
/**
2828
* defines if the channel is private or not and if RLS policies will be used to check data
2929
*/
@@ -154,7 +154,7 @@ export default class RealtimeChannel {
154154
this.params.config = {
155155
...{
156156
broadcast: { ack: false, self: false },
157-
presence: { key: '' },
157+
presence: { key: '', enabled: false },
158158
private: false,
159159
},
160160
...params.config,
@@ -222,24 +222,28 @@ export default class RealtimeChannel {
222222
config: { broadcast, presence, private: isPrivate },
223223
} = this.params
224224

225-
this._onError((e: Error) =>
226-
callback?.(REALTIME_SUBSCRIBE_STATES.CHANNEL_ERROR, e)
227-
)
228-
this._onClose(() => callback?.(REALTIME_SUBSCRIBE_STATES.CLOSED))
225+
const postgres_changes =
226+
this.bindings.postgres_changes?.map((r) => r.filter) ?? []
229227

228+
const presence_enabled = !!this.bindings[REALTIME_LISTEN_TYPES.PRESENCE]
230229
const accessTokenPayload: { access_token?: string } = {}
231230
const config = {
232231
broadcast,
233-
presence,
234-
postgres_changes:
235-
this.bindings.postgres_changes?.map((r) => r.filter) ?? [],
232+
presence: { ...presence, enabled: presence_enabled },
233+
postgres_changes,
236234
private: isPrivate,
237235
}
238236

239237
if (this.socket.accessTokenValue) {
240238
accessTokenPayload.access_token = this.socket.accessTokenValue
241239
}
242240

241+
this._onError((e: Error) =>
242+
callback?.(REALTIME_SUBSCRIBE_STATES.CHANNEL_ERROR, e)
243+
)
244+
245+
this._onClose(() => callback?.(REALTIME_SUBSCRIBE_STATES.CLOSED))
246+
243247
this.updateJoinPayload({ ...{ config }, ...accessTokenPayload })
244248

245249
this.joinedOnce = true
@@ -414,9 +418,19 @@ export default class RealtimeChannel {
414418
): RealtimeChannel
415419
on(
416420
type: `${REALTIME_LISTEN_TYPES}`,
417-
filter: { event: string;[key: string]: string },
421+
filter: { event: string; [key: string]: string },
418422
callback: (payload: any) => void
419423
): RealtimeChannel {
424+
if (
425+
this.state === CHANNEL_STATES.joined &&
426+
type === REALTIME_LISTEN_TYPES.PRESENCE
427+
) {
428+
this.socket.log(
429+
'channel',
430+
`resubscribe to ${this.topic} due to change in presence callbacks on joined channel`
431+
)
432+
this.unsubscribe().then(() => this.subscribe())
433+
}
420434
return this._on(type, filter, callback)
421435
}
422436
/**
@@ -535,10 +549,9 @@ export default class RealtimeChannel {
535549
if (!this._canPush()) {
536550
leavePush.trigger('ok', {})
537551
}
552+
}).finally(() => {
553+
leavePush?.destroy()
538554
})
539-
.finally(() => {
540-
leavePush?.destroy()
541-
})
542555
}
543556
/**
544557
* Teardown the channel.
@@ -649,7 +662,7 @@ export default class RealtimeChannel {
649662
payload.ids?.includes(bindId) &&
650663
(bindEvent === '*' ||
651664
bindEvent?.toLocaleLowerCase() ===
652-
payload.data?.type.toLocaleLowerCase())
665+
payload.data?.type.toLocaleLowerCase())
653666
)
654667
} else {
655668
const bindEvent = bind?.filter?.event?.toLocaleLowerCase()
@@ -714,7 +727,6 @@ export default class RealtimeChannel {
714727
/** @internal */
715728
_on(type: string, filter: { [key: string]: any }, callback: Function) {
716729
const typeLower = type.toLocaleLowerCase()
717-
718730
const binding = {
719731
type: typeLower,
720732
filter: filter,

src/RealtimePresence.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export default class RealtimePresence {
6464
state: RealtimePresenceState = {}
6565
pendingDiffs: RawPresenceDiff[] = []
6666
joinRef: string | null = null
67+
enabled: boolean = false
6768
caller: {
6869
onJoin: PresenceOnJoinCallback
6970
onLeave: PresenceOnLeaveCallback

0 commit comments

Comments
 (0)