I have created my own PHP api for getting and deleting data from database but I want realtime change like firestore database. So how can I listen for data changes in MySQL database? I know stream is the way but how to apply it in Flutter App.
-
Hi, did you find a solution for this yet?Texv– Texv2022-06-13 01:23:46 +00:00Commented Jun 13, 2022 at 1:23
-
@Texv Not yet, I tried to switch to firebase.Abhishek Kumar– Abhishek Kumar2022-06-14 07:41:04 +00:00Commented Jun 14, 2022 at 7:41
-
Very interesting question. I'm wondering the same thing. I think part of the problem is how to listen for MySQL changes (and signal them to PHP) which is asked here: stackoverflow.com/questions/74510554/…Alex– Alex2023-10-16 04:47:52 +00:00Commented Oct 16, 2023 at 4:47
Add a comment
|
2 Answers
Use a replication library and connect to the mysql server as a slave.
This will give you a feed of all database chagnes.
3 Comments
Abhishek Kumar
The library you are suggesting is performing action in every second as I seen by given link on GitHub Readme.md file. That I can do myself but I want realtime changes.
danblack
Apologies, I was assuming it was a realtime replication library like github.com/noplay/python-mysql-replication . I also didn't requite realize it was so new. I'll let you search for a php equivalent or move using to using python (recommended for improving coding anyway).
Abhishek Kumar
Thanks for your recommendation @danblack. I will definitely give it a try on some other day because I am working for a client project right now and I don't have knowledge of python. It will be appreciated if any solution for MySQL DB via PHP.
When i wanted to do a similar thing i created a function that gets the data from database then used a timer to call the function every 3 seconds. After every three seconds the updated ui is rebuilt.
Dont forget to check if the page is mounted and also to dispose the timer
This is What i did
Timer timer = Timer.periodic(const Duration(seconds: 3), (timer) {
setState(() {
refreshProductQuantity();
});
});