supabase / supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
https://supabase.com/
MIT License
656 stars 154 forks source link

feat(functions_client): Add SSE support to invoke method #905

Closed dshukertjr closed 1 month ago

dshukertjr commented 1 month ago

What kind of change does this PR introduce?

Adds support for streaming response of an edge function that returns Server Sent Events.

Fixes https://github.com/supabase/supabase-flutter/issues/894

What is the current behavior?

There is no support for SSE, and developers have to use other libraries to listen to SSE.

What is the new behavior?

Users can listen to SSE like the following:

final response = await supabase.functions.invoke('sse-function');
response.data
    .transform(const Utf8Decoder())
    .listen((val) {
      print(val);
    });

Additional Context

On the web, you would need to pass a custom HTTP client like this:

final fetchClient = FetchClient(mode: RequestMode.cors);
await Supabase.initialize(
  url: supabaseUrl,
  anonKey: supabaseKey,
  httpClient: fetchClient,
);
nietsmmar commented 1 month ago

Additional Context

On Flutter Web, the stream emits data only when the server is done sending the entire events. See the following issue for more info dart-lang/http#337

Thanks for your effort!

But is there no possibility for it to work in web? I managed to make it work in web (with older gotrue/supabase-flutter version) using flutter_client_sse and especially fetch_client. Could there be an option to use fetch_client as alternate http client as darts HttpClient does not support it yet.

Maybe like it is done in here: https://github.com/pratikbaid3/flutter_client_sse/issues/19#issuecomment-1925033427

There is the option to provide an httpClientProvider so I can use fetch_client instead of dart-lang http for web.

This would make it possible to support streaming in web too.

dshukertjr commented 1 month ago

@nietsmmar Actually, just by passing fetch client, I was able to stream SSE on the web!

final client = FetchClient(mode: RequestMode.cors);
await Supabase.initialize(
  url: supabaseUrl,
  anonKey: supabaseKey,
  debug: false,
  httpClient: kIsWeb ? client : null,
);