0
0
Firebasecloud~10 mins

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

Choose your learning style9 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.