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#
- Learn more about the Postgres Changes extension.
- Client Libraries: