Files
supabase/apps/reference/docs/guides/realtime/postgres-changes.mdx
2022-08-18 10:33:08 -04:00

84 lines
2.0 KiB
Plaintext

---
id: postgres-changes
title: Postgres Changes
description: Getting started with Realtime's Postgres Changes feature
---
Supabase Postgres Changes enable clients to listen Postgres database events, specifically INSERT, UPDATE, and DELETE events.
Clients can elect to get changes for all tables in the public schema, a specific table, or a specific column's value.
The postgres changes functionality is supported by supabase-js v2 release candidate.
To listen to all events on all tables in the public schema:
```js
const { createClient } = require('@supabase/supabase-js')
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_KEY
)
/*
The name of the channel can be set to any string.
Event can be either INSERT, UPDATE, DELETE, or * for all events.
*/
const channel = supabase
.channel('db-changes')
.on('postgres_changes', { event: '*', schema: '*' }, (payload) =>
console.log(payload)
)
.subscribe()
```
To listen to INSERT events on the messages table in the public schema:
```js
// Setup
const channel = supabase
.channel('db-changes')
.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'messages' },
(payload) => console.log(payload)
)
.subscribe()
```
To listen to UPDATE events only when a specific column matches a value:
```js
// Setup
const channel = supabase
.channel('db-changes')
.on(
'postgres_changes',
{ event: 'UPDATE', schema: 'public', table: 'messages', filter: 'id=eq.1' },
(payload) => console.log(payload)
)
.subscribe()
```
Clients can also mix and match and listen to different events and schema/tables/filters:
```js
// Setup
const channel = supabase
.channel('db-changes')
.on(
'postgres_changes',
{ event: '*', schema: 'public', table: 'messages', filter: 'id=eq.23' },
(payload) => console.log(payload)
)
.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'users' },
(payload) => console.log(payload)
)
.subscribe()
```