Skip to content
This repository has been archived by the owner on May 13, 2023. It is now read-only.

stream doesn't update realtime #41

Closed
akram-95 opened this issue Aug 16, 2021 · 18 comments
Closed

stream doesn't update realtime #41

akram-95 opened this issue Aug 16, 2021 · 18 comments
Assignees
Labels
bug Something isn't working

Comments

@akram-95
Copy link

akram-95 commented Aug 16, 2021

Bug report

Describe the bug

using stream will my UI just update when i add new value but when i delete value, it will not update

@akram-95 akram-95 added the bug Something isn't working label Aug 16, 2021
@StevePhuc
Copy link

I have the same issue. when insert, new row is insert but the stream is not update
The code I using:

import 'package:flutter/material.dart';
import 'package:supabase_flutter/supabase_flutter.dart';

class ChatScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder(
        stream: Supabase.instance.client
            .from('chat')
            .stream()
            .order('id')
            .limit(30)
            .execute(),
        builder: (ctx, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
          final data = snapshot.data! as List;
          print(data);
          return ListView.builder(
            itemCount: data.length,
            itemBuilder: (ctx, index) => Container(
              padding: EdgeInsets.all(8),
              child: Text('${data[index]['id']} ${data[index]['message']}'),
            ),
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () async {
          final insertResponse =
              await Supabase.instance.client.from('chat').insert([
            {'message': 'new message'}
          ]).execute();
          if (insertResponse.error == null) {
            print('insertResponse.data: ${insertResponse.data}');
          }
        },
      ),
    );
  }
}

already allow realtime for that table

begin; 
  -- remove the realtime publication
  drop publication if exists supabase_realtime; 

  -- re-create the publication but don't enable it for any tables
  create publication supabase_realtime;  
commit;

-- add a table to the publication 
alter publication supabase_realtime add table chat;

image

@dshukertjr
Copy link
Member

dshukertjr commented Aug 17, 2021

Thanks @akram-95 for opening this issue and @StevePhuc for also adding more context!

Could you confirm that you have update and delete realtime events turned on in the Supabase console?

https://app.supabase.io/project/[YOUR_PROJECT_ID]/database/replication

Screen Shot 2021-08-17 at 14 36 06

I apologize for not including instructions around turning realtime on in the readme. I will add it as soon as I can get to it!

@akram-95
Copy link
Author

Hello @dshukertjr , yeah o confirm that I enabled all real time functions

@dshukertjr
Copy link
Member

@akram-95
Sorry, please ignore the previous message that you received in your email inbox.

Could you share the scheme of the table at which you are trying to listen to as stream?

@StevePhuc
Copy link

after update sql with alter table chat replica identity full; it work now. Thanks @dshukertjr

begin; 
  -- remove the realtime publication
  drop publication if exists supabase_realtime; 

  -- re-create the publication but don't enable it for any tables
  create publication supabase_realtime;  
commit;

-- add a table to the publication
alter publication supabase_realtime add table chat;

alter table chat replica identity full;

@dshukertjr
Copy link
Member

dshukertjr commented Aug 17, 2021

@StevePhuc
I'm glad you were able to fix it. I took down my previous post, because I thought that might not have been the issue, but I guess it was!

@akram-95 Could you also try running?

alter table your_table_name replica identity full;

@dshukertjr
Copy link
Member

@StevePhuc
Weird is thing is that I actually do not have identity set to full, and yet am able to receive updates and deletes.

When I run

SELECT CASE relreplident
          WHEN 'd' THEN 'default'
          WHEN 'n' THEN 'nothing'
          WHEN 'f' THEN 'full'
          WHEN 'i' THEN 'index'
       END AS replica_identity
FROM pg_class
WHERE oid = 'mytablename'::regclass;

I get default. Could you maybe try to run the above SQL command to see what you get for tables that you haven't set to full yet?

@StevePhuc
Copy link

StevePhuc commented Aug 17, 2021

@dshukertjr Could you maybe try to run the above SQL command to see what you get for tables that you haven't set to full yet?

image

@akram-95
Copy link
Author

@dshukertjr
Did you mean that ?
image

image

@akram-95
Copy link
Author

@StevePhuc

I'm glad you were able to fix it. I took down my previous post, because I thought that might not have been the issue, but I guess it was!

@akram-95 Could you also try running?

alter table your_table_name replica identity full;

When I did run it , I did get no rows gets

@akram-95
Copy link
Author

@StevePhuc
How did you solve the problem?

@dshukertjr
Copy link
Member

dshukertjr commented Aug 17, 2021

@akram-95
Sorry, for not being clear. Could you show me your table definition?
If you created your table with SQL, then the SQL code would be better, but if you have created from Supabase's console, then a screenshot is okay. Also if you could let me know which column is the primary key, that would be great.
Screen Shot 2021-08-17 at 19 49 33

@StevePhuc
Thank you so much for sharing your result!
I was further investigating this issue, and it seems like there is a bug where stream() should not work when replica identity is set to full. If it's possible, could you share the SQL you used to create your chat table?

For me, it's working when replica identity is set to default.

@akram-95
Copy link
Author

@akram-95

Sorry, for not being clear. Could you show me your table definition?

If you created your table with SQL, then the SQL code would be better, but if you have created from Supabase's console, then a screenshot is okay. Also if you could let me know which column is the primary key, that would be great.

Screen Shot 2021-08-17 at 19 49 33

@StevePhuc

Thank you so much for sharing your result!

I was further investigating this issue, and it seems like there is a bug where stream() should not work when replica identity is set to full. If it's possible, could you share the SQL you used to create your chat table?

For me, it's working when replica identity is set to default.

I did create my table from the console
And here the table definition

image

@dshukertjr
Copy link
Member

@akram-95
I can see that you have an id property, but couldn't really tell the rest. You can take a screenshot by pressing command + Shift + 4 on mac!

Anyway, let me start over. I did create a brand new Supabase project, and was able to receive all Insert, Update, and Delete events with the following steps. Please follow these steps and let me know if they work. It's simple. No need to tweek the replication identity settings or anything.

  1. Go to Database>Replication and check everything .

Screen Shot 2021-08-17 at 20 26 18

2. Click the button inside the red square

Screen Shot 2021-08-17 at 20 26 18

  1. Check that the table at which you want to listen to realtime events are turned on. If not, turn it on.

Screen Shot 2021-08-17 at 20 28 49

  1. Listen to the data using a simple StreamBuilder
StreamBuilder<List<Map<String, dynamic>>>(
  stream: supabase.from('chats').stream().order('id').limit(30).execute(),
  builder: (context, snapshot) {
    if (snapshot.hasError) {
      return Center(child: Text(snapshot.error.toString()));
    }
    if (snapshot.connectionState == ConnectionState.active) {
      final dataList = snapshot.data!;
      return ListView.builder(
        itemCount: dataList.length,
        itemBuilder: (context, index) {
          final data = dataList[index];
          return ListTile(
            title: Text(data['text']),
          );
        },
      );
    } else {
      return Container();
    }
  },
)

@akram-95
Copy link
Author

@dshukertjr I did make it exactly how you told me
It doesn't update the ui
Here is a video of that https://user-images.githubusercontent.com/55693316/129722286-0e1e3581-f460-4d2d-ad8f-11921457178e.MOV

@dshukertjr
Copy link
Member

@akram-95

Thanks for sharing a video.
Okay, in that case, could you make sure that you have primary key for your table? When you create a table, you need to have this check box checked.
Screen Shot 2021-08-17 at 21 04 58

Also, do you have a public repo that I can take a look at where your working code is stored?

@akram-95
Copy link
Author

@dshukertjr
It works now , I could know how to solve it
When we create a table and than enable the real-time , it doesn't work perfectly
I needed to delete it and enable real-time and than create the table and it works now perfect
I wanted to share that with you , maybe it can help you to understand the behaviour better

Now I have another issue , I get this error when I use nested Streambuilder image

@dshukertjr
Copy link
Member

@akram-95
So glad to hear that it is working now. Thanks for sharing the steps you took to make it work. We will work on improving the documentation on how to get started with it.

Thanks for reporting the issue with nested StreamBuilder. Could you actually open another issue for that one?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants