📖 Scenario: You are building a simple chat application where users can see who is currently online. To do this, you will use Supabase's real-time presence feature to track online users.
🎯 Goal: Create a presence system using Supabase that tracks users' online status in real-time.
📋 What You'll Learn
Create a Supabase client instance with the given URL and anon key
Set up a presence channel for user online status
Track when users join and leave the channel
Update the presence state to reflect current online users
💡 Why This Matters
🌍 Real World
Presence tracking is essential in chat apps, collaboration tools, and online games to show who is currently active.
💼 Career
Understanding real-time presence helps in building interactive cloud applications and improves user engagement.
Progress0 / 4 steps
1
Create Supabase client instance
Create a Supabase client instance called supabase using the URL https://xyzcompany.supabase.co and the anon key public-anon-key.
Supabase
Hint
Use createClient from @supabase/supabase-js with the given URL and anon key.
2
Set up presence channel
Create a presence channel called presenceChannel by subscribing to the Supabase real-time channel named online-users.
Supabase
Hint
Use supabase.channel('online-users').subscribe() to create the presence channel.
3
Track user join and leave events
Use presenceChannel to track when users join and leave by adding event handlers for 'presence' events with onJoin and onLeave callbacks. Use variables userId and userName inside the callbacks.
Supabase
Hint
Use presenceChannel.on('presence', { onJoin: ..., onLeave: ... }) to track user presence.
4
Update presence state with track method
Use presenceChannel.track to add the current user with userId set to 'user-123' and userName set to 'Alice'.
Supabase
Hint
Use presenceChannel.track with the given userId and userName to mark the user as online.
Practice
(1/5)
1. What is the main purpose of using presence in Supabase realtime channels?
easy
A. To show which users are currently online in real-time
B. To store user passwords securely
C. To send emails to users
D. To create database backups automatically
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 A
Quick Check:
Presence = Online user tracking [OK]
Hint: Presence means tracking who is online live [OK]
Common Mistakes:
Confusing presence with security features
Thinking presence handles backups
Assuming presence sends emails
2. Which of the following is the correct way to listen for presence updates in Supabase realtime channels?
easy
A. channel.on('presence', callback)
B. channel.on('update', callback)
C. channel.on('message', callback)
D. channel.on('sync', callback)
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 D
Quick Check:
Presence updates use 'sync' event [OK]
Hint: Presence updates come from 'sync' event, not 'presence' [OK]
Common Mistakes:
Using 'presence' event which does not exist
Confusing 'message' event with presence updates
Using 'update' event which is unrelated
3. Given this code snippet, what will be logged when a user joins the presence channel?
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 B
Quick Check:
Must call subscribe() to receive events [OK]
Hint: Always call subscribe() after setting up channel listeners [OK]
Common Mistakes:
Skipping subscribe() call
Using wrong event name
Expecting presenceState() without subscription
5. You want to show a live list of online users with their usernames using Supabase presence. Which approach correctly combines presence tracking and user data fetching?
hard
A. Subscribe to 'update' events and update UI without querying user data
B. Query user profiles once on page load, then listen to 'message' events for updates
C. Listen to 'presence' event, get user IDs from presenceState(), then query user profiles from database
D. Use presenceState() to get usernames directly without extra queries
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 C
Quick Check:
Presence + DB query = live usernames [OK]
Hint: Use presenceState() IDs then query DB for usernames [OK]
Common Mistakes:
Expecting presenceState() to contain usernames
Not querying user profiles after presence update
Listening to wrong events like 'message' or 'update'
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.