Realtime filters let you watch only the data you care about as it changes. This saves time and keeps your app fast.
Realtime filters in Supabase
supabase .from('table_name') .eq('column_name', 'value') .on('INSERT', payload => { /* handle new data */ }) .subscribe()
supabase .from('messages') .eq('room_id', '123') .on('INSERT', payload => console.log(payload)) .subscribe()
supabase .from('orders') .eq('status', 'pending') .on('UPDATE', payload => console.log(payload)) .subscribe()
This code listens only for new chat messages in the room with ID 'room42'. When a new message arrives, it prints it to the console.
import { createClient } from '@supabase/supabase-js' const supabaseUrl = 'https://xyzcompany.supabase.co' const supabaseKey = 'public-anonymous-key' const supabase = createClient(supabaseUrl, supabaseKey) // Subscribe to new chat messages in room 'room42' const subscription = supabase .from('chat_messages') .eq('room_id', 'room42') .on('INSERT', payload => { console.log('New message:', payload.new) }) .subscribe() // Later, to stop listening // subscription.unsubscribe()
Filters reduce unnecessary data and improve app speed.
Always unsubscribe when you no longer need updates to save resources.
Realtime filters let you watch only the data you want.
Use simple filter methods like .eq() to narrow down data.
Remember to unsubscribe when done to keep your app efficient.
Practice
What is the main purpose of using realtime filters in Supabase?
Solution
Step 1: Understand the purpose of realtime filters
Realtime filters let you watch only the data you want by filtering updates.Step 2: Identify the correct purpose among options
Only To receive updates only for specific data changes you care about describes receiving updates for specific data changes, which matches the purpose.Final Answer:
To receive updates only for specific data changes you care about -> Option DQuick Check:
Realtime filters = selective updates [OK]
- Thinking filters speed up initial load by caching
- Confusing filters with backup or encryption features
- Assuming filters encrypt data automatically
Which of the following is the correct syntax to filter realtime updates for rows where status equals 'active'?
supabase.from('tasks').on('INSERT', callback).filter(...)Solution
Step 1: Recall Supabase realtime filter syntax
Supabase uses .match({column: value}) to filter equality in realtime subscriptions.Step 2: Match syntax with options
Only .match({status: 'active'}) uses .match({status: 'active'}), which is correct syntax.Final Answer:
.match({status: 'active'}) -> Option AQuick Check:
.match({}) filters equality in Supabase [OK]
- Using .filter() which is not a Supabase method
- Confusing .where() with SQL but not valid here
- Using .eq() which is for data queries, not realtime subscriptions
Given the code below, what data will the callback receive?
supabase
.from('messages')
.on('UPDATE', payload => console.log(payload.new))
.match({room_id: 5})
.subscribe()Solution
Step 1: Analyze the subscription filter
The code filters updates with .match({room_id: 5}), so only updates where room_id equals 5 are included.Step 2: Understand event type and filter effect
The event is 'UPDATE', so only updated rows matching the filter trigger the callback.Final Answer:
Only updated messages where room_id is 5 -> Option BQuick Check:
Filter + UPDATE event = updates for room 5 only [OK]
- Thinking it includes inserts instead of updates
- Ignoring the .match filter on room_id
- Assuming all rooms trigger callback
What is wrong with this Supabase realtime subscription code?
const subscription = supabase
.from('orders')
.on('INSERT', payload => console.log(payload))
.match({status: 'pending'})
.subscribe()
// Later in code
subscription.unsubscribe()Solution
Step 1: Check method chaining order
Supabase allows chaining .from(), .on(), filters like .match(), then .subscribe() correctly.Step 2: Verify unsubscribe usage
The subscription object returned by .subscribe() has an unsubscribe() method to stop listening.Final Answer:
The unsubscribe call is correct; no error here -> Option CQuick Check:
Correct chaining and unsubscribe usage [OK]
- Thinking .match() must come before .on()
- Trying to unsubscribe from client instead of subscription
- Believing .subscribe() is invalid in Supabase
You want to listen to realtime updates for tasks where priority is either 'high' or 'urgent'. Which approach correctly sets up this filter?
Solution
Step 1: Understand filtering multiple values
Supabase realtime supports .match({col: val}) for equality. No IN or OR for multiple values on the same column in one subscription.Step 2: Evaluate options for multiple priorities
Two separate subscriptions allow filtering each priority correctly and efficiently.Step 3: Why other options fail
B uses invalid .or syntax. C has no .in method. D chaining .match contradicts (cannot match both values).Final Answer:
Use two separate subscriptions with .match({priority: 'high'}) and .match({priority: 'urgent'}) -> Option AQuick Check:
Multiple values on same column need multiple subscriptions [OK]
- Trying to chain multiple .match() filters on same column
- Using invalid .or() syntax for Supabase realtime
- Trying to use .in() like in queries, not supported in realtime
Ready to prove it?
Pass the test and this pattern moves unknown
