Subscribing to database changes lets your app know when data updates happen. This helps keep your app data fresh without asking the database again and again.
Subscribing to database changes in Supabase
const subscription = supabase .from('table_name') .on('INSERT', payload => { console.log('New row:', payload.new) }) .subscribe()
Replace 'table_name' with your actual database table name.
Events can be 'INSERT', 'UPDATE', or 'DELETE' depending on what changes you want to listen for.
const subscription = supabase .from('messages') .on('INSERT', payload => { console.log('New message:', payload.new) }) .subscribe()
const subscription = supabase .from('orders') .on('UPDATE', payload => { console.log('Order updated:', payload.new) }) .subscribe()
const subscription = supabase .from('tasks') .on('DELETE', payload => { console.log('Task deleted:', payload.old) }) .subscribe()
This example connects to Supabase, subscribes to new rows inserted into the 'messages' table, and logs the new message data.
import { createClient } from '@supabase/supabase-js' const supabaseUrl = 'https://xyzcompany.supabase.co' const supabaseKey = 'public-anonymous-key' const supabase = createClient(supabaseUrl, supabaseKey) const subscription = supabase .from('messages') .on('INSERT', payload => { console.log('New message received:', payload.new) }) .subscribe() // To stop listening later: // supabase.removeSubscription(subscription)
Always remove subscriptions when no longer needed to avoid memory leaks.
Payload contains 'new' for INSERT/UPDATE and 'old' for DELETE events.
Make sure your Supabase project has real-time enabled for the tables you subscribe to.
Subscribing to database changes keeps your app data live and fresh.
Use .from('table').on('EVENT', callback).subscribe() to listen for changes.
Remember to clean up subscriptions when done.
Practice
Solution
Step 1: Understand the purpose of subscriptions
Subscriptions listen for database changes and update the app data live.Step 2: Compare options with subscription benefits
Only Keep app data live and updated automatically describes automatic live updates, matching subscription behavior.Final Answer:
Keep app data live and updated automatically -> Option AQuick Check:
Subscriptions = live data updates [OK]
- Thinking subscriptions store data locally
- Confusing subscriptions with manual refresh
- Assuming subscriptions encrypt data
Solution
Step 1: Recall correct Supabase subscription syntax
The correct method is supabase.from('table').on('EVENT', callback).subscribe().Step 2: Match syntax to options
supabase.from('messages').on('INSERT', callback).subscribe() matches this syntax with 'messages' and 'INSERT' event.Final Answer:
supabase.from('messages').on('INSERT', callback).subscribe() -> Option BQuick Check:
Correct syntax = supabase.from('messages').on('INSERT', callback).subscribe() [OK]
- Using .table() instead of .from()
- Listening to wrong event type
- Incorrect method chaining order
supabase.from('tasks').on('INSERT', payload => {
console.log('New task:', payload.new);
}).subscribe()Solution
Step 1: Understand the callback payload on INSERT
The payload contains a 'new' property with the inserted row data as an object.Step 2: Analyze console.log output
Logging 'New task:' plus payload.new prints the object as '[object Object]' in console.Final Answer:
New task: [object Object] with new row data -> Option DQuick Check:
Payload.new logs inserted row object [OK]
- Expecting payload.new to be undefined
- Confusing payload.new with payload.old
- Assuming subscription needs extra setup to log
const subscription = supabase.from('orders').on('DELETE', () => {
console.log('Order deleted');
})Solution
Step 1: Check subscription activation
The .subscribe() method is required to start listening to events.Step 2: Verify event type and callback
'DELETE' is a valid event, and callback can omit payload if unused.Final Answer:
Missing .subscribe() call to activate subscription -> Option AQuick Check:
Always call .subscribe() to activate [OK]
- Forgetting .subscribe() call
- Using wrong event names
- Thinking callback must have parameters
Solution
Step 1: Understand event subscription chaining
Supabase allows chaining .on() calls for different events before .subscribe().Step 2: Evaluate each option's syntax
supabase.from('profiles').on('INSERT', payload => console.log(payload.new)).on('UPDATE', payload => console.log(payload.new)).subscribe() chains .on('INSERT', ...).on('UPDATE', ...) correctly; others use invalid event formats.Final Answer:
supabase.from('profiles').on('INSERT', payload => console.log(payload.new)).on('UPDATE', payload => console.log(payload.new)).subscribe() -> Option CQuick Check:
Chain .on() for multiple events [OK]
- Trying to pass multiple events as array or comma string
- Calling .subscribe() too early
- Not chaining .on() properly
Ready to prove it?
Pass the test and this pattern moves unknown
