Topic-based messaging in Firebase - Time & Space Complexity
When sending messages to many devices using topics, it is important to know how the time to send grows as more devices subscribe.
We want to understand how the number of devices affects the work done by Firebase when sending a topic message.
Analyze the time complexity of the following operation sequence.
const message = {
topic: 'news',
notification: {
title: 'Hello',
body: 'World'
}
};
admin.messaging().send(message)
.then(response => console.log('Sent message:', response))
.catch(error => console.error('Error sending message:', error));
This code sends a notification message to all devices subscribed to the 'news' topic.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Sending one message to the topic via Firebase API.
- How many times: The send call is made once per message, but Firebase internally delivers to all subscribed devices.
As the number of devices subscribed to the topic grows, Firebase must deliver the message to each device.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 send call, 10 device deliveries |
| 100 | 1 send call, 100 device deliveries |
| 1000 | 1 send call, 1000 device deliveries |
Pattern observation: The single API call triggers work that grows linearly with the number of subscribed devices.
Time Complexity: O(n)
This means the work to deliver messages grows in direct proportion to the number of devices subscribed to the topic.
[X] Wrong: "Sending one message to a topic is always a single quick operation regardless of subscribers."
[OK] Correct: Although the API call is one, Firebase must deliver the message to every subscribed device, so the total work grows with the number of devices.
Understanding how message delivery scales with subscribers helps you design efficient notification systems and answer questions about system behavior under load.
"What if we send messages to multiple topics at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand topic-based messaging concept
Topic-based messaging allows grouping devices under a topic to send messages to all subscribers at once.Step 2: Identify the main purpose
The main purpose is to send messages to multiple devices subscribed to the same topic efficiently.Final Answer:
To send messages to multiple devices subscribed to a topic -> Option AQuick Check:
Topic-based messaging = send to many devices [OK]
- Confusing topic messaging with single device messaging
- Thinking it stores messages in database
- Assuming it handles user authentication
Solution
Step 1: Identify subscription function
To add a device to a topic, Firebase provides the subscribeToTopic() function.Step 2: Differentiate from other functions
sendToTopic() sends messages, getToken() gets device token, unsubscribeFromTopic() removes subscription.Final Answer:
subscribeToTopic() -> Option DQuick Check:
Subscribe device = subscribeToTopic() [OK]
- Confusing sendToTopic() with subscription
- Using getToken() to subscribe
- Mixing unsubscribe with subscribe
const message = {
topic: 'news',
notification: { title: 'Hello', body: 'World' }
};
admin.messaging().sendToTopic(message.topic, message.notification);
Solution
Step 1: Analyze sendToTopic usage
The sendToTopic method expects a topic string followed by a payload object. Here, message.topic provides the string 'news' and message.notification provides the payload {title: 'Hello', body: 'World'}.Step 2: Determine the outcome
This correctly sends the notification to all devices subscribed to the 'news' topic.Final Answer:
A notification with title 'Hello' and body 'World' is sent to all devices subscribed to 'news' -> Option AQuick Check:
sendToTopic(topic, payload) sends to subscribers [OK]
- Confusing sendToTopic(topic, payload) with send(full_message_object)
- Thinking it sends only to one device
- Believing the message is stored but not sent
const message = {
topic: 'sports',
notification: { title: 'Game', body: 'Tonight at 8' }
};
admin.messaging().sendToTopic(message);
But it throws an error. What is the likely cause?Solution
Step 1: Check sendToTopic parameter type
In Firebase Admin SDK, sendToTopic expects two parameters: topic string and payload object, not a single message object.Step 2: Identify mismatch
Passing the whole message object as one argument causes an error because the function signature is different.Final Answer:
sendToTopic expects a topic string, not a message object -> Option CQuick Check:
sendToTopic(topic, payload) not sendToTopic(message) [OK]
- Passing message object instead of topic string
- Putting topic inside notification
- Assuming one argument is accepted
Solution
Step 1: Understand topic combination options
Firebase supports sending messages to multiple topics using conditions with logical operators.Step 2: Use condition parameter for AND logic
Using condition: "'news' in topics && 'sports' in topics" sends message only to devices subscribed to both topics.Step 3: Evaluate other options
Sending two messages sends to each topic separately, subscribing to combined topic requires extra setup, filtering manually is inefficient.Final Answer:
Use condition parameter with "'news' in topics && 'sports' in topics" in sendToTopic -> Option BQuick Check:
Use condition for multiple topic targeting [OK]
- Sending separate messages instead of using condition
- Creating combined topic unnecessarily
- Filtering devices manually after sending
Ready to prove it?
Pass the test and this pattern moves unknown
