is there something like a webhook on firebase so that I can increment/decrement presence - firebase

I need to sync the number of online users in each chat room to my elastic search repository (the one that searches for rooms).
Is there a way to attach a webhook server-side onto the connect and disconnect events that fire in firebase so that I can inc and decrement the user counts in my elastic search records?

I would probably use a node.js client for this. That way you can write your client against the same JavaScript SDK that you're already using.
In that client you would subscribe to the same events on the same paths as you would do in a regular front-end client. But instead of updating the UI, you'd just update a node in Firebase itself.
So a regular Firebase client would:
accept user input and send it to Firebase
listen for changes in Firebase an update the user's screen
This node.js client would instead:
listen for changes in Firebase and update the data in Firebase based on that
As Kato mentions: Flashlight is a perfect example of such an application, synchronizing data from Firebase to ElasticSearch and fielding ES queries by queueing them through Firebase.

Related

Flutter Notifications with Firebase Realtime Database

I have a Flutter app integrated with a Firebase Realtime Database.
I want a notification (actually an alarm kind of thing if possible), when an item (lets call it alarm) in the database is set to "true". I always see firebase_messaging plugin for notifications, but I am not sure if I'm supposed to use this plugin despite that my app doesn't have anything to do with messaging.
I am totally new to both Flutter and Firebase, can you tell me how to listen to the database even if the app is not running?
By the way, I am currently building the app for only Android, but I want to build it for IOS too in the future.
Thanks.
You can use/write firebase cloud fucntion. Using firebase cloud function you can watch any document/field and try to write trigger logic like if a field is set to true then this cloud function will throw an notification via firebase messaging.
https://firebase.flutter.dev/docs/functions/overview/
When the user is not actively using the app there is no reliable way to continue to listen to changes in the Firebase Realtime Database. To notify the user of changes to the database in that situation, you'll need to listen for those change on an environment that is always on, and then send a message to the user through Firebase Cloud Messaging.
One environment that is always in is Cloud Functions, which is also part of Firebase, and allows you to run small snippets of JavaScript code on Google's servers in response to things that happen in your Firebase project. The documentation of Cloud Functions for Firebase as example of how to notify the user when something interesting happens in the database:
Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
The function triggers on writes to the Realtime Database path where followers are stored.
The function composes a message to send via FCM.
FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.

Local Notifications vs FCM: Which is right?

I have a fairly straight forward Flutter app which incorporates some "social" features, such as the ability for users to add other users as friends.
When a friend request is "send", a record is added to the Firebase to represent the (pending) friendship. I would like the user "receiving" the friend request to get a notification.
I've looked up a dozen or more posts on using local notifications and FCM, but all I can find are bare-bones PoC style examples. I'm at a loss to understand which methodology is correct for this situation.
Can FCM somehow listen for changes on the database, so when the friend request record is created, it would then push a notification? Or should the receiving user's app be listening for changes to the friend requests and push a local notification?
I'm at a loss for where to start.
Thanks in advance!
The answer is "You have to make your backend listen for changes on the database and send FCM".
If you are using firestore as your database, you can use cloud functions to listen to the changes made to your database. You can read more about it here:
https://firebase.google.com/docs/firestore/extend-with-functions
If you are using a backend of your own, you can make the backend send FCM to a user using the firebase admin sdk for whatever language you are using.

Firestore: how can I get stream of updates from a server side?

tldr: I need to have Google Cloud Functions on my own backend.
I write application, which uses firebase (especially firestore) as a transport layer between my own backend written on nodejs and client applications.
Sometimes, I need to catch some events from client on backend, but i want to avoid perform http queries directly to my backend (because I need to catch offline status, and other problems). It is better to made some changes in firestore documents, catch that changes on my backend and perform some business logic.
As for now, It can be solved with Cloud Functions, but this solution is not acceptable, because of delay between event and function invocation, and lack of invocation order.
Yet another solution, which is currently used in my project, is to making some changes to firestore document, and adding extra document, called "event" to other collection. On a server side, using firebase-admin sdk, I subscribe to that "events" collection and get realtime updates of it.
This work great, but looks like overcomplicated. Is there any way to subscribe from my backend to get all updates of all documents of firestore? Ideal solution is to subscribe to updates, as it done in Cloud Functions: https://firebase.google.com/docs/functions/firestore-events?authuser=0
The client and server SDKs don't have this capability. Cloud Functions is really your only way to get notified of changes in Firestore that match a wildcard pattern.

Adding Mobile Number based login mechanism for auth user in firebase

Can we implement Firebase for an android app, where I am registering user using Mobile number (Similar to Whatsapp). Users will be sent a code by server which is entered by user in android app to validate the user mobile number and registering him on the server.
Question : Can I use the above method in conjunction with Firebase Auth?
I was earlier going to use MongoDB for my project, but since Firebase has SYNC capabilities, it will be a better choice for storing data. Another good reason is as below:
If a client loses its network connection, your app will continue
functioning correctly.
Every client connected to a Firebase database maintains its own
internal version of any active data. When data is written, it's
written to this local version first. The Firebase client then
synchronizes that data with the remote database servers and with other
clients on a "best-effort" basis.
Very NEW to Firebase, just came to know about firebase (through Google 2016 IO).
https://firebase.google.com/docs/database/android/save-data
Firebase hosting is not for server side processing.
It stores static assets of your website as a world-class high availability CDN. So websites hosted here loads very fast. Even in high-availability scenarios.
So you have to do processing at other server which then connects with firebase and stores userinfo in realtime database.
Firebase has put limits on userinfo to be placed in directly for users auth dashboard.
For detailed userinfo, firebase realtime db is the way to go(from your processing backend to firebase realtime db).
Further Reading: What kind of web applications are Firebase not ideal for?

Dynamic Push Notifications

I know that Firebase has recently added support for Push Notifications and while this is a great thing, I only seem to be able to send push notifications manually via the Notifications Panel.
What I'd like to do is to send push notifications within a user scope...Let me explain that. Each User in my App has an account and then each user can join a group. Within this group the user can perform tasks and has a list of chores to do. Now when certain tasks are due for example I want to remind the user of doing it with a push notification. For 1-10 I might be able to pull this off manually, but is there a way to dynamically based on the data in the Database send out Push Notifications?
I know that certain Push Notifications can be created using the Analytics tool such as "Hey you have not visited for 3 days, please come back whatever"... but I'd like to register push notifications such as "I just created a task, this task needs to be done within 3 days. Remind me in 3 days if the task is still not done".
Is this possible with Firebase or do I need to have my own server connecting to Firebase and handling those events?
-xCoder
You need to implement FCM in your client and in a server. Let me put this straight:
First, you need that your client, or app, to register into FCM and get a FCM token that will be used to identify that device uniquely.
Then, store that token wherever you like. It can be into firebase database or other server you may like. I recommend you to store it into firebase if you are using it as a database for your users; that's my case.
Also, you need to implement a http or xmpp server in order to send FCM messages to your registered devices containing the data you are interested in. For example, you can implement a Google App Engine endpoint (can be done with Android Studio and Java) that is quite simple or a NodeJS module, depending on your preferred language.
If you are using Firebase as database you can connect from your server with the appropriate SDK and get the FCM tokens you want from your users, and then send the message to those with data. Don't forget to secure your serve.
The way you implement your server algorithm to send FCM messages depends on your app purposes.
Hope it is clear enough for you. Also you can find all the documentation with a short video that explains the general structure here: https://firebase.google.com/docs/cloud-messaging
You can use cloud functions to trigger on any create, update or delete operation in your database and in the trigger event, you can choose to send in FCM push notifications to the devices of your choice.
Here is the documentation regarding the use and structure of a cloud function: https://firebase.google.com/docs/firestore/extend-with-functions
Hope this helps!

Resources