Home

Subscribing to Database Changes

Supabase allows you to subscribe to real-time changes on your database from your client application.

You can listen to database changes using the Postgres Changes extension.

Demo#

Streaming inserts#

You can use the INSERT event to stream all new rows.

const { createClient } = require('@supabase/supabase-js')

const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY)

const channel = supabase
  .channel('schema-db-changes')
  .on(
    'postgres_changes',
    {
      event: 'INSERT',
      schema: 'public',
    },
    (payload) => console.log(payload)
  )
  .subscribe()

Streaming updates#

You can use the UPDATE event to stream all updated rows.

const { createClient } = require('@supabase/supabase-js')

const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY)

const channel = supabase
  .channel('schema-db-changes')
  .on(
    'postgres_changes',
    {
      event: 'UPDATE',
      schema: 'public',
    },
    (payload) => console.log(payload)
  )
  .subscribe()

More resources#

Need some help?

Not to worry, our specialist engineers are here to help. Submit a support ticket through the Dashboard.