Java – Why do I need to subscribe users to topics in Firebase?

Why do I need to subscribe users to topics in Firebase?… here is a solution to the problem.

Why do I need to subscribe users to topics in Firebase?

I want to implement chat. For example, users A, B, C…
Each user can message each other, which is standard, such as WhatsApp….

And according to the this article, when one user sends a message to another user , I have to subscribe them to a topic.

For example, user A sends a message to user B, and the server creates a topic testTopicName and subscribes them to the topic with this line of code


But the question is, why do I need to subscribe to user topics if I can do it next:

Sends a notification to the user from the server with the name or unique identification of the topic
The user opens this new topic with the unique ID of the topic retrieved from the server

This is the code in the documentation

 mFirebaseAdapter = new FirebaseRecyclerAdapter<FriendlyMessage,
            mFirebaseDatabaseReference.child(testTopicName)) {

Can you explain what are the advantages of using subscriptions, otherwise we need to represent the list of user messages anyway….

If I don’t explain clearly enough, feel free to ask me


There are many ways to target messages sent using Firebase Cloud Messaging. fromdocumentation, you can send to :

  • Topic name
  • Device registration token
  • Device group name

In this post, I decided to associate each user with a topic. This has the following advantages:

  • The application does not have to deal with registration token If you use these tags, you have to store them somewhere, provide them with lookups, etc. This is all possible, but using themes will make the article shorter.
  • If the same user has a chat app on multiple devices, they will receive a notification on each device

And these disadvantages:

In chat applications, the user already has a unique nickname, so this provides a simple user-visible value to map to a topic. If you, Veener, and I are chatting, there will be three topics: /topics/user_aleksey, /topics/user_veener, and /topics/user_puf. Whenever you or Veener mention me in the chat, a notification will be pushed to /topics/user_puf.

There are many ways to map to topics. Which is best depends on your application.

Related Problems and Solutions