Firestore, aggregating data: cloud function vs direct write using the client SDK - google-cloud-firestore

Using Firestore.
Let's say I have a chat app, and once a message is sent, I want to aggregate it to the user's document, which has a "last message" field.
The motivation is to show next to this user's widget the last message sent.
One way to achieve that is using a Cloud Function: listen to changes on the messages collection / document, and copy that data to the user's document.
Another way would be to use the client SDK and write the last message data to the user's document when adding this message to the messages collection / document.
My questions is what are the pros (and cons if any) of using a Cloud Function for that (and in general for aggregating data)?

Related

check weather the user readed data or not in flutter using firestore

I was developing the chat app in flutter using firestore, the users successfully chat one-to-one. but I also want to send the notification to the user that someone sends him/her a message. For that I decided to use the cloud messaging service of firebase, but how can I check that the user read the message or not.
There is nothing built into Cloud Firestore to track which user has read a specific document already.
The two most common approaches that I know of are:
To track for each individual message and each individual user whether they have read it, so by keeping a list of the user IDs in each message document. While this potentially stores a lot of extra data, it is the most direct mapping of your use-case.
In most chat apps though, tracking of what messages each user has read is done as an "up to here". So if the user has read all messages up until a certain one or a certain time, the assumption is that they've also read all the messages before that one/moment.
For this model you can instead just keep a single document ID or timestamp for each user (i.e. readUpTo), instead of having to mark each message for each user that has read it. In your UI, you then determine whether to show each message as unread based on the message ID/timestamp compared to the timestamp of readUpTo.

Firebase cloud functions chess game Swift

I am making a chess app which has a Firebase backend, i am using cloud firestore and cloud functions. basically i am using node 8 to avoid billing issues on cloud functions, i can call and trigger, but i can't wrap my head about how to make an action happen on another device instead of on my own.
After authenticating and logging in, the user gets into a lobby which is a tableViewController and there are shown only the users that are currently logged in.
What i want to achieve is by tapping on a certain row the user that got the tap on it gets an alert shown for him which states whether he accepts the challenge or he declines it. Based on that i proceed to the game or do something else.
The question is though how to make this alert trigger on the other user's device?
Also i've seen some posts that it can be done with snapshotListener on a document, but again i run into the problem of understanding how to trigger the alert on another device. If you've got some other good ideas please share!
Thank you for any feedback!
I think snapshot listeners are really the only way to go. You could use other Firebase services but those aren’t the right tools for the job. Consider creating a collection, maybe call it requests:
[requests]
<userId-userId> (recipientUserId-initiatorUserId)
initiator: string
recipient: string
date: date
Each user has a snapshot listener that listens to this collection where their own userId is equal to recipient. You can add a date field to sort the list by most recent, for example. When a user wants to challenge another user, all they need to do is create a document in this collection:
<userId-userId> (recipientUserId-initiatorUserId)
initiator: myUserId
recipient: theirUserId
date: now
And the recipient's client will instantly see this document.
You could include dressing data in this document, like the initiator's display name or a path to their avatar. But this data may be stale by the time it's rendered so the alternative is to fetch the dressing data from the database using the userId. You could also auto-gen the document ID but if you configure it (like this), it can make operations like deleting simpler. You could also configure the userIds to be alphanumerical so only one request can exist between two users; but if they requested each other at the same time, one would overwrite the other and you'd have to handle that potential edge case. There are lots of things to consider but this is the starting point.

Firestore, FCM, and Cloud functions for a chat app notification system

Using these three tools, I'm trying to think of the best way to notify a user when the other user in a chat sends them a message (using APNs).
Would it be reasonable to store (and always update to newest value) the fcm registration token as a field under the user document in firestore, then in cloud functions, create a trigger to respond to new messages being sent (where each message is a document)? Then in order to determine which device to send it to, we use the registration token field?
Is there a better way to do this?

Firebase Triggers / PubSub / MongoDB

I am using Cloud Functions to handle events, I have some topics that trigger an event such as a write to my document store.
What I would like to do is on a change to my doc store, notify any users interested in that change that there is fresh data.
For example, a news feed. If User A triggers an activity that is written to the store, User B should receive an update that such an activity has taken place, either an instruction to poll new data or just the new event object.
I do not want to use the Firebase Realtime DB as it is a requirement to use MongoDB, however, I believe as Firebase can hook into the events on Google Cloud Functions, I should be able to trigger this still using events?
Is this correct? So far in Firebase I can only find triggers around Realtime DB..
I can sort of achieve this with FCM, however it feels like this is more aimed at giving the user notifications as it requires the user to accept them in the browser, where I want to notify the app itself, not the user.
I do not want to use the Firebase Realtime DB as it is a requirement to use MongoDB, however, I believe as Firebase can hook into the events on Google Cloud Functions, I should be able to trigger this still using events?
Yes, but
Using only Firebase storage triggers is a bit tricky thing to do, as Firebase gives you a Single bucket to work on, so if there is a change in FirebaseStorage(FS) the event will be triggered regardless of in which folder the file is stored.
I can sort of achieve this with FCM, however it feels like this is more aimed at giving the user notifications as it requires the user to accept them in the browser, where I want to notify the app itself, not the user.
Yes, you can use FCM for it, but you will need to store FCM Token of every device in some kind of database(If you dont want to store in Firebase Database) to send it to users.
I want to notify the app itself, not the user.
You can pass payload in FCM through data instead of notification which will be received in onMessageReceived and then you can decide what you want to do with it.
i.e.
var payload = {
data: {
score: "850",
time: "2:45"
}
};

How to read or detect new data in REST Webservice

I've created a REST-webservice with cakePHP for my mobile app. And the idea is to allow customers to load new data in to the system. Now the mobile app will have to check if there is new data available in the system. It can do this by asking the webservice which returns json. Now my question is how can i store the customers data so when i query the webservice it shows which data is new so i can use the new data and leave the old ones?
When querying you will need a way to track which data is already loaded. Depending on the type of data, sending or tracking the last update may suffice (to filter by a creation date), as may a "sent" flag per client on the server side.