Bird
Raised Fist0
Firebasecloud~10 mins

Topic-based messaging in Firebase - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Topic-based messaging
Client subscribes to topic
↓
Topic subscription stored
↓
Server sends message to topic
↓
Message delivered to all subscribed clients
↓
Clients receive and process message
Clients subscribe to a topic. Server sends messages to that topic. All subscribed clients receive the message.
Execution Sample
Firebase
admin.messaging().subscribeToTopic(token, 'news')
admin.messaging().sendToTopic('news', messagePayload)
Subscribe a client to 'news' topic and send a message to all clients subscribed to 'news'.
Process Table
StepActionInputResultNotes
1Subscribe client to topictoken='abc123', topic='news'Subscription storedClient token linked to 'news' topic
2Send message to topictopic='news', message='Hello subscribers!'Message queued for topicServer prepares message for all subscribers
3Deliver messagetopic='news'Message sent to token='abc123'All clients subscribed receive message
4Client receives messagetoken='abc123'Message displayedClient app shows message content
5End--No more actions
💡 All subscribed clients have received the message; process ends.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
subscriptionList{}{'news': ['abc123']}{'news': ['abc123']}{'news': ['abc123']}{'news': ['abc123']}
messageQueue[][][{'topic':'news','message':'Hello subscribers!'}][][]
Key Moments - 2 Insights
Why does the message get delivered only after subscribing?
Because the server sends messages to topics, only clients subscribed to that topic receive messages, as shown in execution_table step 3.
What happens if no clients are subscribed to a topic?
The message is queued but no delivery occurs, so no clients receive it. This is implied by the empty subscriptionList in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the subscriptionList after step 1?
A{'news': []}
B{'news': ['abc123']}
C{}
D['abc123']
💡 Hint
Check the variable_tracker after Step 1 for subscriptionList value.
At which step does the message get sent to the client?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look at execution_table row where message is sent to token='abc123'.
If a client unsubscribes before step 3, what changes in the subscriptionList?
AThe messageQueue is cleared
BThe subscriptionList remains the same
CThe client token is removed from the topic list
DThe topic is deleted
💡 Hint
Consider how subscriptionList tracks clients per topic in variable_tracker.
Concept Snapshot
Topic-based messaging lets clients subscribe to named topics.
Server sends messages to topics, not individual clients.
Only subscribed clients receive messages.
Useful for broadcasting updates efficiently.
Subscribe with subscribeToTopic(token, topic).
Send with sendToTopic(topic, message).
Full Transcript
Topic-based messaging in Firebase works by clients subscribing to topics. When a client subscribes, their token is stored linked to that topic. The server sends messages to a topic, which are then delivered to all clients subscribed to that topic. This allows efficient broadcast messaging. The execution steps show subscription storage, message queuing, delivery to clients, and client reception. Variables track subscriptions and message queues. Key points include that messages only reach subscribed clients and unsubscribed clients do not receive messages. This method is useful for sending updates to groups of users without addressing each individually.

Practice

(1/5)
1. What is the main purpose of topic-based messaging in Firebase Cloud Messaging?
easy
A. To send messages to multiple devices subscribed to a topic
B. To send messages only to a single device
C. To store messages in a database
D. To authenticate users before sending messages

Solution

  1. Step 1: Understand topic-based messaging concept

    Topic-based messaging allows grouping devices under a topic to send messages to all subscribers at once.
  2. Step 2: Identify the main purpose

    The main purpose is to send messages to multiple devices subscribed to the same topic efficiently.
  3. Final Answer:

    To send messages to multiple devices subscribed to a topic -> Option A
  4. Quick Check:

    Topic-based messaging = send to many devices [OK]
Hint: Topic messaging targets groups, not single devices [OK]
Common Mistakes:
  • Confusing topic messaging with single device messaging
  • Thinking it stores messages in database
  • Assuming it handles user authentication
2. Which Firebase function is used to subscribe a device to a topic?
easy
A. unsubscribeFromTopic()
B. sendToTopic()
C. getToken()
D. subscribeToTopic()

Solution

  1. Step 1: Identify subscription function

    To add a device to a topic, Firebase provides the subscribeToTopic() function.
  2. Step 2: Differentiate from other functions

    sendToTopic() sends messages, getToken() gets device token, unsubscribeFromTopic() removes subscription.
  3. Final Answer:

    subscribeToTopic() -> Option D
  4. Quick Check:

    Subscribe device = subscribeToTopic() [OK]
Hint: Subscribe uses subscribeToTopic(), not sendToTopic() [OK]
Common Mistakes:
  • Confusing sendToTopic() with subscription
  • Using getToken() to subscribe
  • Mixing unsubscribe with subscribe
3. Given this code snippet, what will happen?
const message = {
  topic: 'news',
  notification: { title: 'Hello', body: 'World' }
};
admin.messaging().sendToTopic(message.topic, message.notification);
medium
A. A notification with title 'Hello' and body 'World' is sent to all devices subscribed to 'news'
B. An error occurs because sendToTopic requires a message object, not separate topic and notification
C. The message is sent only to one device
D. The message is stored but not sent

Solution

  1. 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'}.
  2. Step 2: Determine the outcome

    This correctly sends the notification to all devices subscribed to the 'news' topic.
  3. Final Answer:

    A notification with title 'Hello' and body 'World' is sent to all devices subscribed to 'news' -> Option A
  4. Quick Check:

    sendToTopic(topic, payload) sends to subscribers [OK]
Hint: sendToTopic(topic_string, payload_object) sends to topic subscribers [OK]
Common Mistakes:
  • Confusing sendToTopic(topic, payload) with send(full_message_object)
  • Thinking it sends only to one device
  • Believing the message is stored but not sent
4. You wrote this code to send a message to a topic:
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?
medium
A. sendToTopic expects the message object, but you passed only the topic string
B. The topic field should be inside the notification object
C. sendToTopic expects a topic string, not a message object
D. sendToTopic expects the message object, but you passed the whole object correctly

Solution

  1. Step 1: Check sendToTopic parameter type

    In Firebase Admin SDK, sendToTopic expects two parameters: topic string and payload object, not a single message object.
  2. Step 2: Identify mismatch

    Passing the whole message object as one argument causes an error because the function signature is different.
  3. Final Answer:

    sendToTopic expects a topic string, not a message object -> Option C
  4. Quick Check:

    sendToTopic(topic, payload) not sendToTopic(message) [OK]
Hint: sendToTopic needs topic string first, then payload [OK]
Common Mistakes:
  • Passing message object instead of topic string
  • Putting topic inside notification
  • Assuming one argument is accepted
5. You want to send a message only to devices subscribed to both 'news' and 'sports' topics. How can you achieve this using Firebase topic messaging?
hard
A. Send two separate messages, one to 'news' and one to 'sports' topics
B. Use condition parameter with "'news' in topics && 'sports' in topics" in sendToTopic
C. Subscribe devices to a combined topic 'news_sports' and send to that topic
D. Send a message to 'news' topic and filter devices manually

Solution

  1. Step 1: Understand topic combination options

    Firebase supports sending messages to multiple topics using conditions with logical operators.
  2. 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.
  3. Step 3: Evaluate other options

    Sending two messages sends to each topic separately, subscribing to combined topic requires extra setup, filtering manually is inefficient.
  4. Final Answer:

    Use condition parameter with "'news' in topics && 'sports' in topics" in sendToTopic -> Option B
  5. Quick Check:

    Use condition for multiple topic targeting [OK]
Hint: Use condition with && for multiple topic targeting [OK]
Common Mistakes:
  • Sending separate messages instead of using condition
  • Creating combined topic unnecessarily
  • Filtering devices manually after sending
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard