Summary
User presence signals do not always clean up correctly upon browser closure, leading to inflated "Online" user counts in the presenceService.
Full Description
The presence.svelte.ts service tracks users in a course. If a user closes their laptop or loses connection, the Supabase record may persist until a server-side timeout. Adding a client-side "Last Will" ensures more accurate real-time data.
How to Resolve
Use the beforeunload browser event in the root layout.
Call a cleanup function in presence.svelte.ts to explicitly leave the channel.
Code Fix In src/lib/ui/TutorsShell.svelte:
<script>
import { onMount } from "svelte";
import { presenceService } from "$lib/services/community/services/presence.svelte";
onMount(() => {
const handleUnload = () => {
presenceService.leaveCourse(); // Explicitly clear presence
};
window.addEventListener("beforeunload", handleUnload);
return () => window.removeEventListener("beforeunload", handleUnload);
});
</script>
In presence.svelte.ts:
export const presenceService = {
// ... existing code
leaveCourse() {
if (this.channel) {
this.channel.unsubscribe();
this.channel = null;
}
}
};
Summary
User presence signals do not always clean up correctly upon browser closure, leading to inflated "Online" user counts in the
presenceService.Full Description
The
presence.svelte.tsservice tracks users in a course. If a user closes their laptop or loses connection, the Supabase record may persist until a server-side timeout. Adding a client-side "Last Will" ensures more accurate real-time data.How to Resolve
Use the
beforeunloadbrowser event in the root layout.Call a cleanup function in
presence.svelte.tsto explicitly leave the channel.Code Fix In
src/lib/ui/TutorsShell.svelte:In
presence.svelte.ts: