Firebase cloud functions chess game Swift - 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.

Related

How to minimize Firestore data? Mutiple fields with boolean type or one Map field type?

I'm making a social media app using flutter and firebase.
I'm making push notifications to alert users on certain actions like somebody is following or send a comment or press like button. Also, there will be a push notification settings page.
I want to let users to choose to turn on and off push notifications on certain actions. For example, a user only wants to get push notification when someone is following on him. Then, the user can just turn off all the other push notifications except following push notification.
What I did was make every fields of every push notifications on certain actions.
For example, I made 4 data fields in user's firebase document for each certain push notifications.
I have a different idea which is making one field of Map type data that contains multiple push notifications like this.
Which way would be the better idea to minimize the size of Firestore data and reduce the cost?
Thank you so much for reading this and if you have other ideas, please let me know :)
Better have different fields - anyway you will receive one dataset, and you don't need to make another "dataset" in this (in same situation you will receive more long response because of additional string markup for inner json)
Also, if you have different fields you can more efficient query it if you need, with less data exchange between client and server

How to Implement "Your contact just joined app" feature

I am building a mobile app (in flutter firebase, but the answer does not have to be firebase specific). And I would like to implement a feature that notifies users whenever anyone from their contact list joins the app. This seems like a very EXPENSIVE feature.
At the top of my head, I see a lambda/cloud function that is triggered everytime a user joins and then searches a database of users and their respective contacts for the existence of the new user's phone number. To me, this solution does not scale well for two reasons: if the number of total users is in the millions and the number of users joining simultaneously is a lot.
My better solution is to get the user's contacts upon joining and then searching a database of current users contacts for any of the phone numbers of the newly joined user.
Is there a solution better than the second one? If so, what is it? If the second solution is standard, what kind of backend storage mechanism provides the best search and retrieval time for a database of users and their respective contacts?
In the case of large users ill not do first solution because that may slow the sign up process instead i will creat a cron job that runs at a specific time or periodically it will get the list of the latest users signed up that day or that hour whatever you prefer then that cron will check the new user if related to any user in the databases and send a notification right away, or a better solution create a temporary table in a database in another server insert the notification informations into the other server, creat another cron job in the second server it will run at a specific time to sendthe notification

Implementing visitor counter in flutter for web

I'm trying to implement a visitor-counter on a website built entirely with flutter-web and I'm trying to accomplish this with nothing but plain dart code and some packages from pub.dev.
But the problem which has been bugging me is that I need to find a way to uniquely identify users based on their browsers or their devices so that I don't end up incrementing the counter for the same person again and again upon a revisit.
So far what I've thought is that I could use firestore for keeping track of the total number of visitors and display the same on the webpage upon startup and use some caching package like dcache or localstorage (like described here) to keep track of users who are re-visiting the same webpage.
Is there any better approach to this problem? Any help would be appreciated 😁
(ps: I have no prior web dev experience)
Since you talk about users, I immediately think of using Firebase Authentication.
There are two ways you could use that here:
With anonymous authentication, you could create a unique ID for each user without requiring them to enter credentials.
If you want to be able to identify the same user on different browsers/devices, they will have to sign in with one of the supported providers (email/password, phone number, email link, facebook, google, etc).
No matter which one you choose, each user will now have a unique ID (called UID in Firebase terms), which you can use to identify them in the database. For example, you could create a document for each user in the database with their UID as the ID of that document. That way you're guaranteed that each user will have no more than a single document.
You'll still need to count the documents, for which I recommend checking out the distributed counter extension.
Also consider if you want to use the Firebase Realtime Database instead of Firestore here, as it might give you a simpler pricing model, and has a built-in presence system. You'd still use Firebase Authentication as before, but just swap the database.
There is a short way instead if you just want to capture number of visitors on your webapp.
I created a collection called "Visits" in firestore and generated auto id inside which I added a field called "count". That's it!
Now you just have to call this method in your first screen's init method :
updateCounter() async {
final cRef = FirebaseFirestore.instance.collection('VISITS');
await cRef
.doc("mS9fCXjvkpREt8LvanSb") //Paste the id which was generated automatically
.set({"count": FieldValue.increment(1)}, SetOptions(merge: true));
}

I have a MongoDB database and I want only a specific user accessing his private data

I have a MongoDB database, express and angular in the frontend.
I already have AUTH setup in a collection and another collection for storing Data.
Right now users can log in and save data, but the data that they save right now is global, meaning I could come and add 1) Learn MongoDB and the second user when they sign in sees the same thing.
I want users to be able to only see the data that he/she posts.
I have been browsing the internet for this specific tutorial but haven't found anything.
If someone could point me in the right direction and a little introduction on it, it would be very helpful.
At its core, this is very very simple. Say, you have a todo list application. So it has Users and Tasks. So the index page for Tasks has this code now (pseudocode ahead):
tasks = db.tasks.find()
render tasks
Naturally, all users see all tasks. What you need to change is now pass id of the logged-in user to the query:
tasks = db.tasks.find({user_id: current_user.id})
render tasks
That's it, now all users see only their own tasks. Do this for all other kinds of private data.

Is it possible to save a custom variable for each user on their account?

I just got starte with programming a Facebook app. I already wrote an app for the VZ-Network, and there they have something called 'Persistant Storage'. Basically its an environment where you can save custom data on each user account. With your app you can read this data from the current user as well as from the users friends. Now I want to port my app to Facebook and my problem is that I didn't find such functionality here yet.
For now I would like to finish and launch this as soon as possible, so it would be nice if I could c&p as much of the code as possible.
Since the data is contains information about participation, at some point I would like to use the Facebook event object. But I was wondering if that could cause problems since it would require to create those events publically in order to use them in my app. Couldn't that lead to legal problems when I create such events with those who actually host the events in the real world? Would I have to ask the hosts to create those events, could I automate this process, or in case they don't have a Facebook account ask them to approve that the app creates the event for them?
I also need to know in what events the users friends participate, so I can't simply save the information on my server, since I don't have the friend info there.
In any case, it seems much easier to me to simply use a list of EventIDs on each user account to check whether or not the user participates in an event.