Skip to content

Realtime Presence

The RealtimePresence component is used to listen to a channel on the Supabase Realtime service to track users’ presence. It uses a svelte store to store and forward the data received from the channel to wrapped components. It also provides two dispatch functions to receive join and leave events. This component is responsible for managing the presence of users in a Supabase Realtime channel. It leverages the presenceStateStore and userStatusStore, which are used to manage the state of the presence channel and the user’s status, respectively.

This component must be a child of the SupabaseApp component to work.

Props:

Prop NameTypeDescription
channelNamestringThe name of the channel to connect to. Defaults to 'any'.
userStatusRecord<string, any> | undefinedThe default user’s status. Defaults to undefined.
channelOptionsRealtimeChannelOptions | undefinedThe options to use when connecting to the channel. Defaults to undefined.

Slots:

Prop NameTypeDescription
stateRealtimePresenceState<any> | nullThe state containing presences data.
realtimeRealtimeClientThe Supabase Realtime client.
errorError | nullThe error, if any, that occurred while connecting to the channel.
channelRealtimeChannel | nullThe Supabase Realtime channel.
updateStatus(status: Record<string, any>) => voidA function that can be used to update the user’s status.

Usage

<RealtimePresence channelName="my-channel">
<div slot="default" let:state let:error let:realtime let:channel let:updateStatus>
{#if error}
<p>Error: {error.message}</p>
{:else if !state}
<p>Loading...</p>
{:else}
<p>Connected to channel {channel?.name}</p>
<p>Number of users: {state?.count}</p>
<button on:click={() => updateStatus({ status: 'online' })}>Go online</button>
<button on:click={() => updateStatus({ status: 'offline' })}>Go offline</button>
{/if}
</div>
</RealtimePresence>