@@ -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 ,
0 commit comments