Function with iOS App

Funtions are the serverless frameworks provided by google-firebase. I started using functions to make my applications scalable. Functions can be used instead of API and lead to quick backend development. Inside the firebase ecosystem functions can access other firebase module. These are similar like lambdas in AWS. With functions you can also move the common code for you application to google cloud and let your application be lightweight.


Setup Problem: From the iOS application using the firebase SDK call a function to send email and phone to firebase. Inside the functions call firestore and get user from the UserList filtered with email&phone params.


PreRequisites:

1. iOS application should be setup and firebase is configured in application.

2. Firebase account should be configured and some startup data should be there in firestore.


GitHub URL: https://github.com/gdsm/findUser_function_iOS



Here are some basic steps to start with functions.


1. Install node and npm on you system. Make sure that you have latest versions of node and npm. And your installation is on correct path ("/usr/bin/node").


2. Install firebase-tools.

$ sudo npm install -g firebase-tools


3. Login to firebase.

$ firebase login

This will switch to browser and ask for firebase login. Login to your firebase/gmail account and your firebase-cli will be activated with your credentials.


4. Some basic set of commands. Complete set of commands are avaliable on firebase github repo.

$ firebase projects:list // list of projects.

$ firebase logout

$ firebase init // initialize a new functions in your current directory.


5. Run firebase init.

this will ask some questions related to firebase project. I used javascript as language and linked function to existing project. Installed npm dependencies.

Command will create a default function in your current directory. Code for function is written inside index.js, firebase.json contains development and deployment configurations for firebase.


6. Now your firebase project is created and open index.js file and write the following initialisation code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

This is the initialisation code for a function and firestore.


7. Below is my function code in JS, change the rool collection name of your own.:

exports.findUser = functions.https.onRequest((request, response) => {

var db = admin.firestore();
const reqData = request.body["data"];
var email = reqData["email"];
var phone = reqData["phone"];
var findUsers = [];
var data = {};
data["status"] = false;

db.collection(<FIRESTORE DB ROOT COLLECTION NAME>).doc("users").get().then(snapshot => {
var usersList = snapshot.get("users_list");
console.log("User list ", usersList);
for (const i in usersList) {
const user = usersList[i];
console.log("traversing user ", user);
if ((user["email_id"] == email) && (user["phone_num"] == phone)) {
findUsers = findUsers.concat(user);
}
}
if (findUsers.length > 0){
data["users"] = findUsers;
data["status"] = true;
}
var retVal = {};
retVal["data"] = data;
response.send(retVal);
});

});


8. Now its time to deploy function to firebase.

$ firebase deploy

That's it, see steps while deploying.


9. Writing the iOS Part, if all iOS project setup is completed then add the below lines to access functions.

import FirebaseCore
import FirebaseFirestore
import FirebaseFunctions

        let fn = Functions.functions(app: <YOUR CONFIGURED FIRAPP>);
        let callable = fn.httpsCallable("findUser");

        let p = NSMutableDictionary()
        p.setValue("user1@gmail.com", forKey: "email");
        p.setValue("1111111111", forKey: "phone");

        callable.call(p) { (httpsCallable, error) in
            if (error != nil){
                print("Error in getting result from function : findUser \(error?.localizedDescription)");
            }
            if let d = httpsCallable?.data {
                print("Received Data \(d)")
            }
        }


10. Go to logs in Firebase/functions and see all logs. You can console.log in your function and see the same logs in function->log console.



That's all. It was my first working with functions, Rather then simply printing no-use HelloWorld , i choose to go with some real scenario for an application to get information from firestore.

Thanks to all audience. Will post more codes and basics about functions with close to real time environment.