supabase / realtime-js

An isomorphic Javascript client for Supabase Realtime server.

Home Page:https://supabase.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Realtime token gets expired chanel auto closes

MariuzM opened this issue · comments

commented

When I'm using Realtime and have window opened for some time i see that Realtime ws is is checking for my token, but at some point token gets expired and the channel closes thus I'm losing Realtime connection.

The only way is todo hard page refresh that goes and fetches new token, but it does not make sense why cant Realtime do the same?

I think solution should be for Realtime detect when token expired and refresh it and then send refresh_token event back to client of it to update its token

I would really like to avoid writing a custom interval to check token and refresh so then on next ws token check it will get updated one.

Channel shutting down with message: access token has expired: [message: "Invalid token", claim: "exp", claim_val: 1692674245]
commented

Or at lease send any event about channel being closed so i can do something on client

Hey MariuzM, Can I Work on this bug?

.subscribe(status=>console.log(status)) is a status event I use to restart realtime when it closes/errors as it does alot on mobile power down or browser tabs in the background.

Not sure on the refresh token part. If your tab is in the background the timers get slowed down it is possible the refresh timer does not happen in time. For sure realtime loses heartbeat after 5 minutes on some browsers in background tab.

.subscribe(status=>console.log(status)) is a status event I use to restart realtime when it closes/errors as it does alot on mobile power down or browser tabs in the background.

Not sure on the refresh token part. If your tab is in the background the timers get slowed down it is possible the refresh timer does not happen in time. For sure realtime loses heartbeat after 5 minutes on some browsers in background tab.

@GaryAustin1 could you please expand on this? I assume I'm missing something, but wouldn't that only run on the first subscription? How do you then detect a disconnect and resubscribe? I seem to be losing realtime as soon as I switch to another tab and back, and I'm assuming it's the subscription that's closing.

@chasebank Sorry for the late reply. These two repositories of mine show the tab issue and handling restarting based on visibility of the tab. If a tab is in the background for several minutes browsers go into low power mode and slow the timers which breaks realtime's heartbeat.

https://github.com/GaryAustin1/Realtime2
https://github.com/orgs/supabase/discussions/5641

These are not "final packaged" solutions, just tests and show some code that avoids the issue.

In case if someone encounter this issue on the server side, there is an option for token auto refresh:

import { createServerClient as createSSRClient } from '@supabase/ssr'
import type { SupabaseClient } from '@supabase/supabase-js'

export function createServerClient (supabaseUrl: string, anonKey: string): SupabaseClient {
    const container: Record<string, any> = {}

    const cookies = {
        get: (key) => {
            return container[key]
        },
        set: (key, value, options) => {
            container[key] = value
        },
        remove: (key, options) => {
            container[key] = undefined
        }
    }

    return createSSRClient(
        supabaseUrl,
        anonKey,
        {
            cookies,
            auth: {
                autoRefreshToken: true
            }
        }
    )
}

thank you @tvaliasek ! yes this is the advised way of tackling this issue as such I will close the issue.