Realtime filters in Supabase - Time & Space Complexity
When using realtime filters in Supabase, we want to know how the number of updates or messages affects performance.
We ask: How does the work grow as more data matches the filter?
Analyze the time complexity of subscribing to filtered realtime updates.
const subscription = supabase
.channel('public:messages')
.on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages', filter: 'room_id=eq.42' }, payload => {
console.log('New message:', payload.new)
})
.subscribe()
This code listens for new messages only in room 42 and reacts when they arrive.
Look at what happens repeatedly as new data arrives.
- Primary operation: Receiving filtered realtime events from the server.
- How many times: Once per matching new row inserted in the database.
Each new matching message triggers one event call.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 event calls |
| 100 | 100 event calls |
| 1000 | 1000 event calls |
Pattern observation: The number of event calls grows directly with the number of new matching rows.
Time Complexity: O(n)
This means the work grows linearly with the number of new matching events received.
[X] Wrong: "The subscription only uses one API call no matter how many events happen."
[OK] Correct: Each matching event triggers a separate message from the server, so the total work grows with the number of events.
Understanding how realtime filters scale helps you design responsive apps that handle growing data smoothly.
What if we changed the filter to listen to all rooms instead of just one? How would the time complexity change?
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
