Skip to content

Realtime WebSocket upgrade returns 401 for a user JWT accepted by Auth #619

Closed

Description

@lucacivale

Client.InitializeAsync() fails in Realtime.ConnectAsync() with:

The server returned status code '401' when status code '101' was expected.

Repro:

  • Create a console application (Supabase version 8.6.0)
  • Enter url, public key and email and password of an existing confirmed user.
  • Run console application
  • The application will sign in successfully
  • The custom DefaultSupabaseSessionHandler will store the token and refresh token
  • Exit the console application
  • Run the application again
  • Realtime.ConnectAsync() will fail even though the token is not invalid and a refresh token is available
string url = "";
string key = "";
string email = "";
string password = "";

Client client = new(url, key, new SupabaseOptions
{
    AutoRefreshToken = true,
    SessionHandler = new DefaultSupabaseSessionHandler(Path.Combine(AppContext.BaseDirectory, "supabase-session.json")),
});

try
{
    await client.Auth.LoadSessionAsync();
    await client.Auth.RetrieveSessionAsync();
    await client.Realtime.ConnectAsync();

    if (client.Auth.CurrentSession is null)
    {
        await client.Auth.SignIn(email, password);
    }
    return 0;
}
catch (Exception exception)
{
    Console.Error.WriteLine($"{exception}");
    return 1;
}
internal sealed class DefaultSupabaseSessionHandler(string filePath) : IGotrueSessionPersistence<Session>
{
    private readonly string _filePath = Path.GetFullPath(filePath);

    public void SaveSession(Session session)
    {
        Directory.CreateDirectory(Path.GetDirectoryName(_filePath)!);

        // Replace the file only after serialization and writing have completed.
        string temporaryPath = _filePath + ".tmp";
        File.WriteAllText(temporaryPath, JsonSerializer.Serialize(session));
        File.Move(temporaryPath, _filePath, overwrite: true);
    }

    public Session? LoadSession()
    {
        return File.Exists(_filePath)
            ? JsonSerializer.Deserialize<Session>(File.ReadAllText(_filePath))
            : null;
    }

    public void DestroySession()
    {
        File.Delete(_filePath);
    }
}

Workaround

I don't really know what is happening under the hood but if you remove the Authorization from the Realtime headers it works

Func<Dictionary<string, string>>? getRealtimeHeaders = client.Realtime.GetHeaders;
client.Realtime.GetHeaders = () =>
{
    Dictionary<string, string> headers = getRealtimeHeaders?.Invoke() ?? [];
    headers.Remove("Authorization");
    return headers;
};

Activity

  1. self-assigned this
    on Nov 2, 2026
  2. Tr00d commented on Nov 2, 2026

    @Tr00d
    Contributor

    @lucacivale Thanks for raising this one. I'll get back to you when a fix is released.

  3. lucacivale commented on Nov 2, 2026

    @lucacivale
    Author

    @Tr00d thanks for the quick fix!
    I'm fairly new to supabase. Is there any release cadence for this project?

  4. Tr00d commented on Nov 2, 2026

    @Tr00d
    Contributor

    @lucacivale The release cycle isn't predefined, it's basically on-demand.
    There are 2 PRs I want to include as well in the next release, so I expect this to be done today 👍

  5. Tr00d commented on Nov 2, 2026

    @Tr00d
    Contributor

    @lucacivale v8.8.0 has been released and contains the fix.
    It will be available in a couple of minutes, as soon as NuGet finishes indexing.

    Let me know how it goes on your side

  6. lucacivale commented on Nov 2, 2026

    @lucacivale
    Author

    @Tr00d thanks!
    Yes, works like a charm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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