1

I am make chat feature with Flutter and Firestore backend.

Every message is new document in Firestore collection with UID and text field. Chat is 1:1 and random so no know who user will talk to before enter chat. DocID in chat collection are all auto-id.

I have read can use topics to manage send notification. This should be easier than use individual device fcm token.

Anyone know how to implement use topic for this random 1:1 chat app?

FlutterFirebase
  • 1,379
  • 1
  • 15
  • 38
  • 3
    Are you sure topics are going to be easier? Topic messaging doesn't sound like a good fit for 1 to 1 messaging. The whole idea of a topic is to broadcast a message to many devices all listening in on that topic. The whole idea of token messaging is to send messages to one device (user). – Doug Stevenson Dec 29 '18 at 21:33
  • I am interest to see this approach because I have read this is possible. Maybe easier because no need maintain tokens in db – FlutterFirebase Dec 29 '18 at 21:37
  • I guess it's possible, but you'd still have to end up storing something for each user that identifies the unique topic that sends messages to the other user. You might as well just store the token as is typical. – Doug Stevenson Dec 29 '18 at 21:41

2 Answers2

1

You can definitely use a separate topic for each 1:1 conversation, for example with the naming scheme I described here: Best way to manage Chat channels in Firebase. But there are some things to consider which, as Doug already pointed out in his comment, leads most developers to not solely use FCM for their chat apps.

For example: FCM topics are not secured. This means that anyone who finds out the topic ID can subscribe to it, and thus overhear the 1:1 conversation. And while you can generate topics that are hard to guess, you should not rely on not knowing the topic ID as a security mechanism.

Another reason to consider alternatives is that FCM messages are transient: once they are delivered there is no longer any trace of them. With your current Firestore implementation you can query the database to get all messages to show, while with a pure FCM implementation you will have to build your own database (if that is required for your app).

For these reasons most chat apps I know of use a combination of FCM (for push notifications) and an online database (for persistence) as their backend services.

Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Thanks for reply! If use individual FCM token (not topic) each user token must be update in Firestore from client. Is there no issue with notification not being sent because token is refresh but client app not update field in Firestore? For example if have network issue – FlutterFirebase Jan 02 '19 at 20:48
0

I found Frank's comment really interesting about the "sorted userID composed key". I would probably use that as the chat key in the database (realtime/firestore), however, for the notifications I think I would still use a topic for each user - this way I would be able to avoid notifying the user who posted the message. If that wouldn't be an issue, then just go for just one topic per chatroom.

Also mentioning Frank, I would probably use extra keys in all topic names to make them really difficult to guess. (but add that later so you don't get distracted with non core stuff)

In this answer you have an example of how to post the notifications with a onCreate trigger to a topic (from a functions backend).

In the flutter code, you can use subscribeToTopic from the firebase_messaging plugin to start listening to a topic.

Note: if your app will support user log-off [probably it will :)], then you'll also have to delete the token in the device to avoid receiving notifications from the last logged user.

Feu
  • 4,133
  • 1
  • 23
  • 48
  • Thanks!! Are you use this in production app? I am interest in you experience. For example Firebase docs say: 'Topic messages are optimized for throughput rather than latency'. You have experience latency? – FlutterFirebase Jan 02 '19 at 20:42