Questions on Firebase Cloud Messages topics vs groups - firebase

I made two databases in my project.
db1 contains a collection of unique tokens for each device that's currently wishing to receive notifications in my application.
db2 contains entities which will often update.
I've setup cloud functions to detect update triggers for db2 then send a message to a single device.
In my cloud function I'm reading and stashing in a collection all the unique tokens from db1.
How can I extend the cloud function to send the message to the collection of unique tokens from db1?
Should I use topics or groups for this? I don't know how big my audience can get.
I've looked at the firebase docs but can't quite figure out which to use.

Related

How can I write to the Realtime Database, Cloud Storage and Firestore at the same time using transactions? [duplicate]

This question already has answers here:
Couple Firebase Firestore and Firebase Storage calls together into a batch?
(2 answers)
Closed last month.
I'm developing an app in Flutter, I have a method called **CreateUser **which takes as parameters the user information, his profile picture, and a list of strings, I need to save the information in the Realtime Database, the picture in the Cloud Storage and the list in the Firestore.
I would like all these operations to be successful, if one of these should fail then I would like the others to undo the data they wrote. How can I implement the rollback of the other operations? Can I use transactions?
I've tried using transactions but I'm not sure if I can use them on different Databases.
I need to save the information in the Realtime Database, the picture in the Cloud Storage, and the list in the Firestore.
That's indeed possible, by performing one operation, right after another, only when the operation succeeds. For example, as soon as the operation for writing data to the Realtime Database completes, then inside the callback, perform the addition of the image to Storage. As soon as the addition of the image to Storage succeeds, perform the last operation of writing the data to Firestore.
I would like all these operations to be successful, if one of these should fail then I would like the others to undo the data they wrote.
There is no built-in mechanism for that. If you thought you can add to a batch operation, a Realtime Database write operation, a Firebase Storage file upload
and Firestore write operation and be sure that all three are complete, so you can have consistent data, please note that this is not possible. These operations are a part of different Firebase services and unfortunately, at the moment I'm writing this answer there is no way you can make them atomic, meaning all succeed or all fail with an exception.
How can I implement the rollback of the other operations?
You have to write code for that because none of the Firebase products support cross-product transactional operations. To solve this, you'll have to nest the calls during your write/upload operations and handle the error if the next operation fails. This means that you either have to delete the data from the Realtime Database and the file from Storage if the write operation in Firestore fails. Or only delete the data from the Realtime Database if the file upload to Storage fails.
But note, at some point in time, there will be a failure that the client can't roll back one of the delete operations. The most common approach for these inevitable failures which might happen is to make your code robust by handling exceptions and performing occasional cleanups in both places, Firebase Storage and Firestore, considering that the first operation is the one that writes data to the Realtime Database.
As discussed with the Firebase engineers, the reason is quite clear. Even if the Realtime Database and Cloud Firestore are both Firebase products, they are still different products. Besides that, Firebase Storage is a service within Google Cloud. So now, 2023-01-12 there is no way you we can do that. Hopefully, it will be available in the near future.
Can I use transactions?
No, and that's for the exact same reason as above.
One way I might address this, is to use the Firestore document write operation to trigger a Workflow [1] that can handle the three operations and rollback depending on failure state. That way you can also have a constant transaction record follow the process.
If you wanted to provide app feedback say to the user, you could have your app. wait for a DB record of completion (or error) get written and based on that report back to the user.
[1] https://cloud.google.com/firestore/docs/solutions/workflows

Create a named push-subscription to a Google Cloud Pub/Sub topic using firebase cloud functions

I know I can create an "anonymous" subscription for a topic like this:
functions.pubsub.topic('topic-name').onPublish(console.log)
But this doesn't allow me to distribute different messages to multiple listeners on a single subscription and, more importantly, it doesn't allow me to bind a subscription to a dead-letter topic, because, as I understand it, this subscriptions are ephemeral (i.e. they are tied to the lifecycle of the Firebase Cloud Function).
Is there a way to achieve this without creating a "real" Google Cloud Function and only using Firebase?

Will firebase be able to handle huge traffic?

So I have been coding a realtime chat app with flutter and firebase. Apparently there is a simultaneous concurrent connection limit of 200k on the RTDB, if my app reaches these limits, firebase suggests that I shard my database.
The problem with that is, that I cannot completely separate data from one database instance to another, as it's a realtime social media app, and one user should be able to access other user's information such as name, bio etc.
What I want to know is that, should I stick with firebase for this, or should I go with some other database.
You can always access data from multiple shards on your app as long as you know which shard does the required data belong to. You can also use Firestore along with Realtime database to suit your needs. Checkout the following answer for an example:
How to shard data Realtime Database for chat app?
This stores all user profiles and basic information about the chats (including which shard is the conversation stored in) which makes it easier to query list of chats of a particular user(s) from Firestore and then read messages from realtime DB.

Which firebase database to use for chat applicatoin, Firestore or Realtime Database?

I'm building an app which uses Firestore for storing most data. The app has a chat functionality and I was considering using Realtime Database for that. What are the benefits of using Firebase Firestore vs Realtime Database for this chat functionality? If there is no difference, should I use Firestore for everything?
P.S. I have already read the firebase comparison of the two https://firebase.google.com/docs/database/rtdb-vs-firestore and I am still not sure which way to go about this.
FB RTDB was designed for a chat application but is not so great for more than simple querying. Firestore was developed to improve the querying requirements and is newer. Newer doesn't necessarily mean better, depends on the use case. Their pricing models are very different, so you need to understand how your use case will be charged.
You can use both of course. They can work well together but if a simple chat requirement is all you need, I would use RTDB.
PS. The unique keys generated in RTDB for each new record are automatically in chronological order, which relates back to it being designed for a chat app. There is a caveat though, the chat messages may still get out of order because the keys are generated on the device and if the device clocks are slightly out and messages are being exchanged rapidly then you may get a miss timing. The way round this is to write each record with a property of server time...and use that to sort the chat messages. Hope that helps your decision.
PPS. RTDB charges for data storage volumes and data download volumes. Firestore charges for storage and db reads and writes. There will be a lot of the latter in a chat app so I would recommend running some what-if scenarios in Excel.

Does Firebase create a topic when I subscribe to a non existing one?

I started working with Firebase in order to simplify the use of Push Notifications of my app. Searching around the Docs I found the possibility to send a notification to a group of devices through a topic which the devices are subscribed to.
My doubt comes out here. When I subscribe the devices (using firebaseToken) to the topic I want to use, does this topic creates implicitly if it's not created?
If it doesn't, when and how can I create a topic to use it later?
I'm using Firebase Cloud Store an Firebase Messaging.
You don't need to create a topic in order to use topic messaging. It just works the way you expect as long as the server and clients all agree on the name of the topic.
Firebase Cloud Messaging isn't related to Cloud Firestore in any way, other than that they are both Firebase products and are seen together in documentation and the console.

Resources