Add flag in RTDB to record host online/offline
Since Firebase cloud functions can listen to realtime database write/read/delete changes, when the host signed in/out, we can change a value in database, for example:Write function which listens to the flag update and deploy to cloud
Open a terminal:
- run command: npm install -g firebase-tools
- run command: firebase login
- create a folder and run command: firebase init
- open index.js and add the following function
- deploy function to the cloud via command: firebase deploy --only functions
Test and check the logs in Functions panel:
For simplicity, I just hardcoded Sylvia as the host and when signing in as Sylvia, the cloud function should be triggered, you should be able to see the logs in firebase console.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPCGRqOABHMNdyEB0ZSwcDdE_sebWwO7Tnax8Nze63ecPf71_baKtp_v0L953DN4ONRdZtni6iLnP0FqqrBIey9rysG0KHa3B5jo6sA0ypVwtDv_5W8RokAPzJewf8Yg7OOJ-jAT2Bo-k/s640/Screen+Shot+2019-02-25+at+7.25.07+pm.png)
Besides of realtime database, cloud function can listen on a list of other services. In the next chapter, I will use cloud function with cloud messaging.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdGcvM961Zh5weo0fyNdLQoXgKE5HLAgLWXLhTAq2sTPqIZ3NrGhQSTMNcjvM8_YztEUzcocd30M9r2UbYJKeC2Fb53grXfWJxpEpYwvhBixcxCGN1UkmNIhweaRKaTCs8zBv87oVll1g/s400/Screen+Shot+2019-02-25+at+7.31.10+pm.png)
All chapters:
Realtime chat iOS demo (1) - Firebase Realtime Database
Realtime chat iOS demo (2) - Firebase Remote Config
Realtime chat iOS demo (3) - Firebase Cloud Storage
Realtime chat iOS demo (4) - Firebase Cloud Function
Realtime chat iOS demo (5) - Firebase Cloud Messaging
Source code:
TarotChatRoom in Github
No comments:
Post a Comment