supabase-community / realtime-csharp

A C# client library for supabase/realtime.
https://supabase-community.github.io/realtime-csharp/api/Supabase.Realtime.Client.html
MIT License
70 stars 12 forks source link

Error connecting Supabase Realtime : Failed to start Websocket client, error: 'The server returned status code '403' when status code '101' was expected #37

Closed trogalko closed 7 months ago

trogalko commented 1 year ago

Bug report

Describe the bug

I used self-hosted supabase (docker) with custom domain name and SSL from LestEncrypt when I tried to connect using this code : var endpoint = "wss://mydomain.id/realtime/v1/websocket?apikey="+ GlobalVariables.supabase_anon_key; Supabase.Realtime.Client client = new Supabase.Realtime.Client(endpoint); await client.ConnectAsync(); I got this error : Failed to start Websocket client, error: 'The server returned status code '403' when status code '101' was expected I can access my supabase studio using my domain name

System information

Additional context

Add any other context about the problem here.

acupofjose commented 1 year ago

Try this:

var endpoint = "wss://mydmain.tld/realtime/v1/websocket";
var headers = new Dictionary<string, object> {
  { "apikey", GlobalVariables.supabase_anon_key },
  { "Authorization", $"Bearer {GlobalVariables.supabase_anon_key}" }
};

var options = new Supabase.Realtime.ClientOptions { Headers = headers };
var client = new Supabase.Realtime.Client(endpoint, options);

await client.ConnectAsync();
trogalko commented 1 year ago

Still same error, first error was because headers are readonly, so I remove the readonly attribute, rebuild it and overwrite the dll in the .nuget package folder. Then the same error happen : Failed to start Websocket client, error: 'The server returned status code '403' when status code '101' was expected

acupofjose commented 1 year ago

Hm. I'm able to duplicate it following the direction here: https://supabase.com/docs/guides/self-hosting/docker to set up a self-hosted environment. It seems that setting a new ANON_KEY and SERVICE_KEY works for using the REST apis, but not for accessing realtime. I can only assume that a configuration option has changed with realtime server... but I'm a little stuck at the moment figuring out what option changed.

trogalko commented 1 year ago

Yes that's right, I only unable to connect to supabase realtime with SSL (wss), I'll try with SSL disable (ws)

acupofjose commented 1 year ago

Okay, I think I may have found the issue... I'm not sure that the JWT generator works correctly here: https://supabase.com/docs/guides/self-hosting/docker#generate-api-keys

Try using a different generator (like on jwt.io) being sure that the header matches the following:

Header:

{
  "alg": "HS256",
  "typ": "JWT"
}

Then be sure that you change the .env to reflect the new JWTs and restart the server.

I was able to get the server to connect after adjusting the keys.