Presence
firekitPresence uses the Realtime Database’s connection state to track user presence. It writes an online/offline record to RTDB and cleans it up via onDisconnect.
Initialize
Section titled “Initialize”import { firekitPresence } from 'svelte-firekit';import { onMount } from 'svelte';
onMount(async () => { await firekitPresence.initialize(user, { sessionTTL: 30 * 60_000, // 30 minutes in ms trackDeviceInfo: true, geolocation: { enabled: true, type: 'browser', requireConsent: true, }, });});Options
Section titled “Options”| Option | Type | Default | Description |
|---|---|---|---|
sessionTTL | number | 1800000 | Session time-to-live in ms |
trackDeviceInfo | boolean | false | Include device/browser info in the presence record |
geolocation.enabled | boolean | false | Track approximate location |
geolocation.type | 'browser' | 'ip' | 'browser' | How to obtain location |
geolocation.requireConsent | boolean | true | Only track after user consent |
Set presence status
Section titled “Set presence status”await firekitPresence.setPresence('online');await firekitPresence.setPresence('away');await firekitPresence.setPresence('offline');Read presence stats
Section titled “Read presence stats”const stats = firekitPresence.getStats();
console.log(stats.onlineSessions); // number of active sessionsconsole.log(stats.totalSessions); // all sessions including offlineconsole.log(stats.uniqueDevices); // number of distinct device typesDispose
Section titled “Dispose”Call dispose when the user signs out or the component that owns presence is destroyed:
await firekitPresence.dispose();This removes the user’s presence record and cleans up the onDisconnect handler.
Full example
Section titled “Full example”<script lang="ts"> import { firekitPresence, firekitUser } from 'svelte-firekit'; import { onMount, onDestroy } from 'svelte';
onMount(async () => { if (firekitUser.isAuthenticated && firekitUser.user) { await firekitPresence.initialize(firekitUser.user, { sessionTTL: 15 * 60_000, trackDeviceInfo: true, }); } });
onDestroy(async () => { await firekitPresence.dispose(); });</script>