📖 Scenario: You are building a simple web app that needs to react instantly when new messages are added to a chat database table. Instead of refreshing the page, you want to listen for new messages as they arrive.
🎯 Goal: Create a Supabase client, subscribe to new inserts on the messages table, and log the new message data when it arrives.
📋 What You'll Learn
Create a Supabase client connected to your project URL and anon key
Subscribe to INSERT events on the messages table
Log the new message data inside the subscription callback
💡 Why This Matters
🌍 Real World
Real-time apps like chat, notifications, or live dashboards need to react instantly to database changes without refreshing the page.
💼 Career
Understanding how to subscribe to database changes is key for building responsive, event-driven applications in modern cloud environments.
Progress0 / 4 steps
1
Create Supabase client
Create a variable called supabase by calling createClient with the exact URL 'https://xyzcompany.supabase.co' and anon key 'public-anon-key'.
Supabase
Hint
Use createClient from @supabase/supabase-js with the given URL and key.
2
Set up subscription to new messages
Create a variable called subscription by calling supabase.channel('public:messages') and chaining .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages' }, callback) where callback is a function that receives payload and logs payload.new.
Supabase
Hint
Use supabase.channel with the channel name 'public:messages'. Then use .on with event 'INSERT' on the messages table. The callback logs payload.new.
3
Subscribe to the channel
Call subscription.subscribe() to start listening for new message inserts.
Supabase
Hint
Call subscribe() on the subscription variable to activate the listener.
4
Unsubscribe when done
Add a line to unsubscribe from the channel by calling subscription.unsubscribe() when you no longer want to listen for new messages.
Supabase
Hint
Call unsubscribe() on the subscription variable to stop listening.
Practice
(1/5)
1. What does subscribing to database changes in Supabase help your app do?
easy
A. Keep app data live and updated automatically
B. Manually refresh data by reloading the page
C. Store data permanently on the client device
D. Encrypt data before sending to the database
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 A
Quick Check:
Subscriptions = live data updates [OK]
Hint: Subscriptions keep data live without manual refresh [OK]
Common Mistakes:
Thinking subscriptions store data locally
Confusing subscriptions with manual refresh
Assuming subscriptions encrypt data
2. Which of the following is the correct syntax to subscribe to INSERT events on the 'messages' table in Supabase?
easy
A. supabase.from('messages').on('UPDATE', callback).subscribe()
B. supabase.from('messages').on('INSERT', callback).subscribe()
C. supabase.table('messages').listen('INSERT', callback).subscribe()
D. supabase.listen('messages').on('INSERT', callback).subscribe()
A. Missing .subscribe() call to activate subscription
B. Wrong event type; should be 'REMOVE' instead of 'DELETE'
C. Callback function must receive a payload parameter
D. Subscription must be assigned to a variable named 'sub'
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 A
Quick Check:
Always call .subscribe() to activate [OK]
Hint: Always end with .subscribe() to start listening [OK]
Common Mistakes:
Forgetting .subscribe() call
Using wrong event names
Thinking callback must have parameters
5. You want to subscribe to both INSERT and UPDATE events on the 'profiles' table and log the changed row each time. Which code correctly achieves this?
hard
A. supabase.from('profiles').on('INSERT,UPDATE', payload => console.log(payload.new)).subscribe()
B. supabase.from('profiles').on(['INSERT', 'UPDATE'], payload => console.log(payload.new)).subscribe()
C. supabase.from('profiles').on('INSERT', payload => console.log(payload.new)).on('UPDATE', payload => console.log(payload.new)).subscribe()
D. supabase.from('profiles').on('INSERT').on('UPDATE', payload => console.log(payload.new)).subscribe()
Solution
Step 1: Understand event subscription chaining
Supabase allows chaining .on() calls for different events before .subscribe().