Presence for online status in Supabase - Time & Space Complexity
We want to understand how the time it takes to track online users grows as more users connect or disconnect.
Specifically, how does the system handle many users updating their online status?
Analyze the time complexity of the following operation sequence.
// User connects
const { data, error } = await supabase
.from('presence')
.upsert({ user_id: userId, status: 'online' })
// User disconnects
await supabase
.from('presence')
.delete()
.eq('user_id', userId)
// Listen for presence changes
const subscription = supabase
.channel('presence')
.on('postgres_changes', { event: '*', schema: 'public', table: 'presence' }, payload => {
// update UI with online users
})
.subscribe()
This sequence updates a user's online status and listens for changes to show who is online.
- Primary operation: Upsert or delete calls to the 'presence' table for each user status change.
- How many times: Once per user connection or disconnection event.
- Secondary operation: Real-time subscription listens for all presence changes.
- Dominant operation: The upsert/delete calls scale with the number of users changing status.
Each user connecting or disconnecting triggers one database operation.
| Input Size (n users) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 upsert or delete calls |
| 100 | About 100 upsert or delete calls |
| 1000 | About 1000 upsert or delete calls |
Pattern observation: The number of operations grows directly with the number of users changing status.
Time Complexity: O(n)
This means the time to update presence grows linearly with the number of users changing their online status.
[X] Wrong: "Updating one user's status updates all users at once, so time stays constant."
[OK] Correct: Each user's status update is a separate operation, so more users mean more updates and more time.
Understanding how presence updates scale helps you design systems that handle many users smoothly and keep interfaces responsive.
"What if we batch multiple user status updates into a single database call? How would the time complexity change?"
Practice
Solution
Step 1: Understand presence feature
Presence in Supabase is designed to track and show who is online at any moment.Step 2: Identify main use case
It helps applications display real-time online user lists, not for security or backups.Final Answer:
To show which users are currently online in real-time -> Option AQuick Check:
Presence = Online user tracking [OK]
- Confusing presence with security features
- Thinking presence handles backups
- Assuming presence sends emails
Solution
Step 1: Recall Supabase presence event names
Supabase presence uses the 'sync' event to notify when presence data updates.Step 2: Match event to correct listener
The 'sync' event triggers the callback with the updated online user list.Final Answer:
channel.on('sync', callback) -> Option DQuick Check:
Presence updates use 'sync' event [OK]
- Using 'presence' event which does not exist
- Confusing 'message' event with presence updates
- Using 'update' event which is unrelated
const channel = supabase.channel('online-users').on('presence', () => {
console.log(channel.presenceState())
}).subscribe()Solution
Step 1: Understand presenceState() method
presenceState() returns the current presence data, showing online users grouped by user ID.Step 2: Analyze code behavior on 'presence' event
When a user joins, 'presence' triggers and logs the updated presence state object.Final Answer:
An object showing all currently online users -> Option AQuick Check:
presenceState() = online users object [OK]
- Thinking presenceState() is undefined
- Expecting a string message instead of object
- Assuming it returns an empty array always
const channel = supabase.channel('online-users')
channel.on('presence', () => {
console.log('Users online:', channel.presenceState())
})
What is the likely problem?Solution
Step 1: Check channel subscription
Without calling subscribe(), the channel does not connect and receive events.Step 2: Confirm event and method correctness
'presence' event and presenceState() are correct, so missing subscribe() is the issue.Final Answer:
You forgot to call subscribe() on the channel -> Option BQuick Check:
Must call subscribe() to receive events [OK]
- Skipping subscribe() call
- Using wrong event name
- Expecting presenceState() without subscription
Solution
Step 1: Understand presenceState() data
presenceState() gives user IDs and metadata, but not full usernames.Step 2: Combine presence with database queries
After getting IDs from presenceState(), query the database to fetch usernames for display.Final Answer:
Listen to 'presence' event, get user IDs from presenceState(), then query user profiles from database -> Option CQuick Check:
Presence + DB query = live usernames [OK]
- Expecting presenceState() to contain usernames
- Not querying user profiles after presence update
- Listening to wrong events like 'message' or 'update'
Ready to prove it?
Pass the test and this pattern moves unknown
