Monday 25 February 2019

Realtime chat iOS demo (4) - Firebase Cloud Function

One more feature to my demo which will adopt Firebase cloud function along with realtime database and remote notification. Once I login to the room as the host I want all the guests to be notified by remote push notification. In this chapter, I will solve the first part making the cloud function triggered on sign in/out. 


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.




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.




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