1. 18
    Dynamically Update UI with Database Changes using Supabase Realtime
    3m

Dynamically Update UI with Database Changes using Supabase Realtime

InstructorJon Meyers

Share this video with your friends

Send Tweet

Supabase allows us to subscribe to changes in the database, and update our UI, without the user needing to refresh the page. In this lesson, we create a subscription for postgres_changes, listening for any change events - insert, update or delete - on our tweets table.

Additionally, we call the router.refresh() function to re-run our Server Components, fetching fresh data from Supabase.

Code Snippets

Subscribe to database changes

const channel = supabase
  .channel("realtime tweets")
  .on(
    "postgres_changes",
    {
      event: "*",
      schema: "public",
      table: "tweets",
    },
    (payload) => {
      router.refresh();
    }
  )
  .subscribe();

Resources

Jens Mikkelsen
~ 9 months ago

I'm not sure if this was mentioned in the video, but the revalidatePath call in new-tweet.tsx is no longer necessary after subscribing. Maybe it makes the update more quick but still no longer strictly necessary.