Skip to content

Course Analytics "Ghost" Sessions #1035

@codeCraft-Ritik

Description

@codeCraft-Ritik

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;
    }
  }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions