0

I am already using Cloud Messaging feature in Firebase and utilized the sendToTopic() legacy method of sending notification to all subscribers but I've seen a limitation using topic in my app. Now I want to manage my way of sending and receiving notification by sending notification to each device using the registered device token in user's document which stored as map object. I will iterate to each device token and use sendToDevice() to send notification to each device.

I have now a function lets call it new_added that triggers whenever new document is added in a collection. Now every time new_added function gets called, this will iterate to each document in Users collection and write a new document under Notification collection. The structure would be this Users (collection) > uid (document) > Notifications > doc. Every new added item under Notification collection will trigger a function in server. This operation is too heavy specially if there is a million number of users, does this kind of operation can be perform in server side using Cloud Function within 540 seconds which is said to be the maximum runtime of a function after gets trigger? I really want it to work this way. Is there any tool that will help to minimize the operation?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Mihae Kheel
  • 2,441
  • 3
  • 14
  • 38
  • 1
    If you have to read a collection of a million documents, that doesn't sound like a very good thing to do in a Cloud Function. Also it sounds very expensive. – Doug Stevenson Feb 17 '19 at 09:21
  • Then is it okay to block a notification with an if else condition in client app as my another solution? – Mihae Kheel Feb 17 '19 at 10:09

1 Answers1

0

If sounds like you're trying to implement your own FCM messaging fan-out, sending the same message to many users. This is similar to what FCM topic messaging already does, to I'd definitely consider using that for the moment.

But if you want to implement it yourself, consider how to optimize it for the underlying system. Since you're storing the tokens in Firestore, the number of documents you read is a key factor in the cost.

A way to reduce the number of documents you read could be to store the tokens for a group of devices into a single document. For example, you could create a document for each "topic", and add tokens to that doc as they are written to the database. Then when you need to send a message to all devices for a topic, you simply read that topic-document, and get all tokens in one go. This becomes especially simple if you name the document after the topic for which it contains the tokens, e.g. mytopic-tokens.

The main problem with this approach is that a document can be no bigger than 1Mb. Say that tokens are at most 256 bytes (they seem to be 152-162 characters), you can store 4000 tokens in a document. If you have more tokens, you will need to create multiple documents. A simple naming scheme can go a long way here, such as mytopic-tokens-1, mytopic-tokens-2, etc. You can get all these documents with a single range-query in Firestore.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807