📖 Scenario: You are building a real-time chat application using Supabase. You want to notify all connected clients when a new message is sent by broadcasting a custom event.
🎯 Goal: Create a Supabase client, configure a channel to broadcast a custom event called new_message, and send a message payload to all subscribers.
📋 What You'll Learn
Create a Supabase client instance with the exact variable name supabase.
Create a channel named chat_channel using supabase.channel().
Broadcast a custom event called new_message with a payload containing { text: 'Hello, world!' }.
Subscribe to the channel to enable broadcasting.
💡 Why This Matters
🌍 Real World
Real-time applications like chat apps, live notifications, or collaborative tools use custom event broadcasting to update all users instantly.
💼 Career
Understanding how to broadcast and subscribe to custom events in Supabase is essential for building scalable real-time cloud applications.
Progress0 / 4 steps
1
Create Supabase client
Create a Supabase client instance called supabase using createClient with the exact URL 'https://xyzcompany.supabase.co' and the API key 'public-anonymous-key'.
Supabase
Hint
Use createClient from @supabase/supabase-js with the given URL and key.
2
Create a channel for broadcasting
Create a channel called chat_channel using supabase.channel('chat_channel').
Supabase
Hint
Use the channel method on supabase with the exact name 'chat_channel'.
3
Broadcast the custom event
Use chat_channel.broadcast to send a custom event called new_message with the payload { text: 'Hello, world!' }.
Supabase
Hint
Call broadcast on chat_channel with the event name and payload.
4
Subscribe to the channel
Call chat_channel.subscribe() to enable the channel for broadcasting events.
Supabase
Hint
Call subscribe() on chat_channel to activate it.
Practice
(1/5)
1. What is the main purpose of using broadcast in Supabase custom events?
easy
A. To let different parts of your app communicate instantly
B. To store data permanently in the database
C. To create user authentication tokens
D. To schedule background jobs automatically
Solution
Step 1: Understand the role of broadcast
Broadcast is used to send messages instantly between parts of an app without delay.
Step 2: Compare with other options
Storing data, authentication, and scheduling jobs are unrelated to broadcast messaging.
Final Answer:
To let different parts of your app communicate instantly -> Option A
Quick Check:
Broadcast = instant app communication [OK]
Hint: Broadcast means instant messaging inside your app [OK]
Common Mistakes:
Confusing broadcast with database storage
Thinking broadcast handles authentication
Assuming broadcast schedules tasks
2. Which of the following is the correct way to listen for a broadcast event on a Supabase channel?
easy
A. channel.subscribe('broadcast', callbackFunction)
B. The payload object must include a 'channel' property
C. The channel name must be 'broadcast' to send broadcast events
D. The listener is set after sending the broadcast, so it misses the message
Solution
Step 1: Check the order of send and listen
The code sends the broadcast before setting the listener, so the listener misses the event.
Step 2: Validate other options
Send does not require a callback; channel name can be any string; payload does not need a 'channel' property.
Final Answer:
The listener is set after sending the broadcast, so it misses the message -> Option D
Quick Check:
Listener must be set before send to catch events [OK]
Hint: Set listener before sending broadcast [OK]
Common Mistakes:
Sending before listening causes missed events
Thinking send needs a callback
Assuming channel name must be 'broadcast'
5. You want to broadcast a custom event to multiple parts of your app grouped under a channel named 'notifications'. Which steps correctly implement this using Supabase's broadcast feature?
hard
A. Use subscribe('notifications') to listen and emit('broadcast') to send events
B. Create a channel 'notifications', set listeners with on('broadcast'), then send events with send({ type: 'broadcast', ... })
C. Create multiple channels for each listener, then send events individually to each channel
D. Broadcast events without creating a channel; listeners listen globally by default
Solution
Step 1: Group listeners and senders with a channel
Create a channel named 'notifications' to group app parts.
Step 2: Use on('broadcast') to listen and send() to broadcast
Listeners use on('broadcast') and senders use send({ type: 'broadcast', ... }) to communicate.
Final Answer:
Create a channel 'notifications', set listeners with on('broadcast'), then send events with send({ type: 'broadcast', ... }) -> Option B