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
697 stars 164 forks source link

Real-time Message Detection Not Triggering Callback #960

Closed asaki-daisuke closed 2 months ago

asaki-daisuke commented 2 months ago

Describe the bug I am working on a Flutter application that uses Supabase for real-time messaging. The issue is that the callback for new messages is not being triggered despite the subscription to the real-time channel being successful.

To Reproduce Use the following code and insert a new record into the Supabase table.

Expected behavior

class ChatBloc extends Bloc<ChatEvent, ChatState> {
  ChatBloc(
      {required this.supabaseClient})
      : super(const ChatState()) {
    _listenToNewMessages();
  }

  final SupabaseClient supabaseClient;
  late final RealtimeChannel realtimeChannel;

  void _listenToNewMessages() {
    final realtimeChannel = supabaseClient.channel('public:messages');
    realtimeChannel
      .onPostgresChanges(
          event: PostgresChangeEvent.all,
          schema: 'public',
          table: 'messages',
          callback: (payload) {
            print('New message payload received: $payload');
          })
      .subscribe((status, error) {
        print('Subscribed to messages channel: $status error: $error');
     });

This prints the following. Subscribed to messages channel: RealtimeSubscribeStatus.subscribed error: null It seems to successfully instantiate the realtimeChannel. However, when I inserted a new message from the web browser application (React app) or directly through the Supabase console, it did not trigger the callback.

Version (please complete the following information): On macOS ├── supabase_flutter 2.5.6 │ ├── supabase 2.2.2 │ │ ├── functions_client 2.2.0 │ │ ├── gotrue 2.8.1 │ │ ├── postgrest 2.1.2 │ │ ├── realtime_client 2.1.0 │ │ ├── storage_client 2.0.2

dshukertjr commented 2 months ago

Please check if you have enabled realtime on messages table.

asaki-daisuke commented 2 months ago

It is enabled. Also, it works fine with the same table with the React app.

dshukertjr commented 2 months ago

@asaki-daisuke Could it be an RLS issue? Have you disabled RLS on the table to see if it works?

asaki-daisuke commented 2 months ago

Oh, it was a RLS issue! Thank you.