supabase-community / supabase-csharp

A C# Client library for Supabase
https://github.com/supabase-community/supabase-csharp/wiki
MIT License
484 stars 50 forks source link

Initialize supabase client #3

Closed elrhomariyounes closed 2 years ago

elrhomariyounes commented 3 years ago

Bug report

Describe the bug

Hello, I have an issue, is there any workaround, the thing is if I want to have a service with the supabase client as a property how can I initialize the supabase client since the Initialize method is async for example :

public class MyService : IMyService
{
    private readonly Supabase.Client _supabaseClient;

    //Some methods
}

I can't initialize the supabaseClient in the constructor

acupofjose commented 3 years ago

Would this work?

public class MyService
{
        public readonly Supabase.Client client;

        public MyService()
        {
            client = Supabase.Client.Initialize(supabaseUrl, supabaseKey, options);
        }
}
elrhomariyounes commented 3 years ago

Hello @acupofjose, Thank you for the quick reply. Here the Initialize will return a

Task<Supabase.Client>
public class MyService
{
  public readonly Supabase.Client client;
  public MyService()
  {
     // Get Result
     client = Supabase.Client.Initialize(supabaseUrl, supabaseKey, options).Result;
  }
}
acupofjose commented 3 years ago

Will that work? Or do you think we should transition to:

Supabase.Client Initialize(string, supabaseUrl, string supabaseKey, SupabaseOptions options);
// and
Task<Supabase.Client> InitializeAsync(string, supabaseUrl, string supabaseKey, SupabaseOptions options)
elrhomariyounes commented 3 years ago

Yeah good idea! and if I'm not wrong the Postgrest client is not initialized when calling Supabase.Client.Initialize

acupofjose commented 3 years ago

That's correct, it's not supposed to be - it is supposed to be initialized on each call to From() (this is the way that the supabase-js library does it.)

acupofjose commented 3 years ago

Aaaalllllrighty. Changes have been pushed to both gotrue-csharp@2.0.0 and supabase-csharp@0.1.4-prerelease that default to a synchronous API for initialization. The previous initializer can be found with Client.InitializeAsync(....). Let me know if that works for you!

elrhomariyounes commented 3 years ago

Hello, I tried to make a sample todo project using Blazor Web Assembly and Supabase Todo Quick Start.

I made an AuthenticationService where I initialize the supabase client in the constructor, but the application crushed immediately when navigating to the component where my AuthenticationService is injected you can check the sample project to reproduce here.

  1. Navigate to counter component an exception is thrown
    Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Cannot wait on monitors on this runtime.
    System.PlatformNotSupportedException: Cannot wait on monitors on this runtime.
acupofjose commented 3 years ago

@elrhomariyounes sorry I've been pretty slow on responding to this. I'm not particularly familiar with Blazor - but it looks like it the exception is thrown from blocking the main thread on an async call. Is it possible to change to using InitializeAsync() for your use case? The alternative is to change the gotrue API to require a synchronous call for SessionRetriever, SessionPersistor, and SessionDestroyer. I'm not sure I want to require that.

Any thoughts?

elrhomariyounes commented 3 years ago

Hello @acupofjose, Hope you're doing great.

I've been trying this days to figure out a solution without changing all this methods to run synchronously, but unfortunately no idea, I tried using InitializeAsync but I have to call .Result to set the supabase client value and this will result to a blocking call same as Initialize.

gostan99 commented 3 years ago

Hello @acupofjose, Hope you're doing great.

I've been trying this days to figure out a solution without changing all this methods to run synchronously, but unfortunately no idea, I tried using InitializeAsync but I have to call .Result to set the supabase client value and this will result to a blocking call same as Initialize.

I have the same issue. Don't know how to make it works.

acupofjose commented 3 years ago

@gostan99 thanks for the report! Are you also attempting to use this on Blazor?

gostan99 commented 3 years ago

@gostan99 thanks for the report! Are you also attempting to use this on Blazor?

yes I am attempting to use this on Blazor. Seems that Blazor Webassembly does not support Websocket I think.

acupofjose commented 3 years ago

Found the problem! The Client now defaults to not initializing Realtime functionality unless specified. I've also removed problematic Task.Result calls that were causing blazor issues.

Available in 0.1.5-prerelease

syedqutub commented 3 years ago

Hi Everyone, @acupofjose i was having trouble using to use the Realtime feature of Supabase on Blazor WebAssembly (Platform not supported exception) and found this thread. i do know that Blazor WASM supports System.Net.WebSocket.ClientWebSocket (they wrote specific implementation for blazor wasm) but i am not sure if WebSocketSharp supports Blazor WASM. is it possible to use supabase realtime on blazor wasm. if not is it feasable to replace the WebSocketSharp implementation with with ClientWebSocket in Supabae.Realtime. i am willing to contribute if needed!!

acupofjose commented 3 years ago

@syedqutub - Blazor is not a framework I'm familiar with, I've only worked on it with debugging on this project. As far as I've seen, WebsocketSharp does not support Blazor WASM. Supabase would require a different client that is either specific for Blazor or more generalized that WebSocketSharp (with Blazor support). I gladly welcome any contributions you have!

syedqutub commented 3 years ago

@acupofjose Thanks alot for the reply!. i do know that System.Net.WebSockets.ClientWebSocket c;lass is supported on Blazor WASM. so am playing around with it. if i manage to come up with an implementation for the Supabase's Realtime Socket class. i will let you know!.