Broadcast for custom events in Supabase - Time & Space Complexity
When using Supabase to broadcast custom events, it's important to understand how the number of events affects the work done behind the scenes.
We want to know how the time to send events grows as we broadcast more messages.
Analyze the time complexity of broadcasting multiple custom events.
const channel = supabase.channel('custom-events');
for (let i = 0; i < n; i++) {
channel.send({
type: 'broadcast',
event: 'my_event',
payload: { message: `Event number ${i}` }
});
}
This code sends n custom broadcast events one after another on the same channel.
Look at what repeats as we send events:
- Primary operation: Sending a broadcast event via
channel.send()API call. - How many times: Exactly
ntimes, once per event in the loop.
Each event requires a separate send call, so as n grows, the number of send operations grows the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 send calls |
| 100 | 100 send calls |
| 1000 | 1000 send calls |
Pattern observation: The number of send operations grows directly with the number of events.
Time Complexity: O(n)
This means the time to broadcast events grows linearly as you send more events.
[X] Wrong: "Sending multiple events at once takes the same time as sending one event."
[OK] Correct: Each event requires a separate send operation, so more events mean more work and more time.
Understanding how event broadcasting scales helps you design efficient real-time features and shows you can reason about system behavior as load grows.
"What if we batch multiple events into a single send call? How would the time complexity change?"
Practice
broadcast in Supabase custom events?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 AQuick Check:
Broadcast = instant app communication [OK]
- Confusing broadcast with database storage
- Thinking broadcast handles authentication
- Assuming broadcast schedules tasks
Solution
Step 1: Recall Supabase broadcast listening syntax
The correct method to listen for broadcast events ison('broadcast', callback).Step 2: Check other options
Methods likelisten,receive, andsubscribeare not valid for broadcast events in Supabase.Final Answer:
channel.on('broadcast', callbackFunction) -> Option CQuick Check:
Listen with on('broadcast') = channel.on('broadcast', callbackFunction) [OK]
- Using listen() instead of on()
- Confusing subscribe() with on()
- Trying receive() which is invalid
const channel = supabase.channel('chat');
channel.on('broadcast', (payload) => {
console.log('Received:', payload.message);
});
channel.send({ type: 'broadcast', message: 'Hello!' });Solution
Step 1: Understand the broadcast sending and listening
The channel sends a broadcast with message 'Hello!'. The listener logs payload.message.Step 2: Trace the output
The listener receives the payload and logs 'Received: Hello!'.Final Answer:
Received: Hello! -> Option AQuick Check:
Broadcast send triggers on() callback with message [OK]
- Expecting no output because of missing subscription
- Confusing payload structure causing undefined
- Assuming send() method does not exist
const channel = supabase.channel('updates');
channel.send({ type: 'broadcast', message: 'Update!' });
channel.on('broadcast', (payload) => {
console.log(payload.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 DQuick Check:
Listener must be set before send to catch events [OK]
- Sending before listening causes missed events
- Thinking send needs a callback
- Assuming channel name must be 'broadcast'
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 useon('broadcast')and senders usesend({ type: 'broadcast', ... })to communicate.Final Answer:
Create a channel 'notifications', set listeners with on('broadcast'), then send events with send({ type: 'broadcast', ... }) -> Option BQuick Check:
Channel + on('broadcast') + send() = correct broadcast setup [OK]
- Creating multiple channels unnecessarily
- Using subscribe() or emit() which are invalid
- Assuming broadcast works without channels
Ready to prove it?
Pass the test and this pattern moves unknown
