Db Table Changes Tells LiveView

So I have a table of MediaItems and when that list changes, say new entries, I’d like LiveView to know about that so that the Timeline updates automatically. How to know when the table updates? I’ve seen supabase but that appears to be for JS.

I’m just trying to learn LiveView and everything I’ve found about is so far in terms of examples is user-initiated actions changing state rather than server events. In another forum someone recommended looking into Change Data Capture but I think if I followed the CDC path I would get bogged-down in the details of CDC rather than develop the frontend.

Check postgresql triggers, You can do this with them… :slight_smile:

That will tell you how to have PG notify the application. You’ll want to see a LiveView PubSub example for the interface update.

1 Like

Yes thanks I think this is what I was looking for. My presumption is that I can do a broadcast() once I get that table update event. I won’t mark it as “solution” until I implement it though.

Yes - the wire-up for listening to broadcasts is shown here…

If the code/feature doing the original database inserts is part of the same application that is presenting these items in a LiveView, I’d reach for Phoenix.PubSub. You can have your LiveView processes subscribe to a topic for new insertions, and have the area that performs the insertions emit an event on that topic or topics.

1 Like

For insertion it’s fine, but I did not find a good way to detect cascading deletion without triggers.