Receive inserts, updates and deletes from your tables as they happen.
#Publish a table
Add the table to the stitchdb_realtime publication, the built-in publication every project has. In Studio, tick Enable Realtime when creating a table or use Database → Publications; or run:
SQL
alter publication stitchdb_realtime add table public.todos;#Subscribe
JavaScript
stitchdb
.channel('todos')
.on('postgres_changes', { event: '*', schema: 'public', table: 'todos' }, (payload) => {
console.log(payload.eventType, payload.new)
})
.subscribe()#Filter events
JavaScript
stitchdb.channel('my-todos')
.on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'todos', filter: 'user_id=eq.' + user.id }, (p) => add(p.new))
.on('postgres_changes', { event: 'DELETE', schema: 'public', table: 'todos' }, (p) => remove(p.old.id))
.subscribe()event is INSERT, UPDATE, DELETE or *. Filters support eq, neq, lt, lte, gt, gte and in. For UPDATE and DELETE, payload.old holds only the primary key unless you run alter table public.todos replica identity full;.
#Security
Row Level Security decides which changes each user receives: a subscriber only gets rows their policies let them select. A published table with RLS off sends every row to every subscriber, and Advisors warns about it.