Skip to content

Assertion failed: You can only track your presence after subscribing to the channel. Did you forget to call channel.subscribe()? #622

Open
@arbyruns

Description

@arbyruns

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

I've noticed that an increase of assert crash Assertion failed: You can only track your presence after subscribing to the channel. Did you forget to call channel.subscribe()? even though I'm calling subscribe()

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Launch app
  2. Ensure presence is configured for when the user launches the app

Expected behavior

Crash does not occur

Screenshots

image

System information

SupabaseSDK 2.20.5

Additional context

My function

    func setUserPresence() async {
        var isTracked = false

        let deviceId = UIDevice.current.identifierForVendor?.uuidString ?? "unknown-user"

        let realtime = supaBaseClient.realtimeV2
        let userPresence = supaBaseClient.channel("user_presence")
        let presenceStream = userPresence.presenceChange()
        await userPresence.subscribe()

        for await presence in presenceStream {
            // Handle joins
            if !presence.joins.isEmpty && !isTracked {
                await userPresence.track(
                    state: [
                        "user": .string(deviceId),
                        "onlineAt": .double(Date().timeIntervalSince1970)
                    ]
                )
                isTracked = true // Update the state
            }

            // Handle leaves
            if !presence.leaves.isEmpty && isTracked {
                await userPresence.untrack()
                isTracked = false // Update the state
            }
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions