Skip to content

FB4D Reference IFirebaseFunctions

Christoph Schneider edited this page Mar 17, 2022 · 12 revisions

Interface IFirebaseFunctions

For understanding the concept of the Cloud Functions read this first: Cloud Functions Documentation from Firebase. Afterward, you have to write your first Firebase Function and deploy it in your Firebase Project: see also Firebase Functions Getting Started Documentation.

FB4D offers the following interface for calling a cloud function from your application, passing the function's parameters, and retrieving the result of the called function. Depending on the region of the Google server where the function was uploaded, this region must also be specified before calling any function.

You can create an instance of _IFirebaseFunction _with the following constructor or you can use the class factory _IFirebaseConfiguration _for this purpose.

constructor TFirebaseFunctions.Create(const ProjectID: string;
  Auth: IFirebaseAuthentication = nil;
  const ServerRegion: string = cRegionUSCent1);

The following server locations are predefined in constants:

const
  cRegionUSCent1 = 'us-central1';             // Iowa
  cRegionUSEast1 = 'us-east1';                // South Carolina
  cRegionUSEast4 = 'us-east4';                // Nothern Virginia
  cRegionUSWest2 = 'us-west2';                // Los Angeles
  cRegionUSWest3 = 'us-west3';                // Salt Lake City
  cRegionUSWest4 = 'us-west4';                // Las Vegas
  cRegionUSNoEa1 = 'northamerica-northeast1'; // Montreal
  cRegionUSSoEa1 = 'southamerica-east1';      // Sao Paulo
  cRegionEUWest1 = 'europe-west1';            // Belgium
  cRegionEUWest2 = 'europe-west2';            // London
  cRegionEUWest3 = 'europe-west3';            // Frankfurt
  cRegionEUWest6 = 'europe-west6';            // Zürich
  cRegionEUCent2 = 'europe-central2';         // Warsaw
  cRegionAUSoEa1 = 'australia-southeast1';    // Sydney
  cRegionASEast1 = 'asia-east1';              // Taiwan
  cRegionASEast2 = 'asia-east2';              // Hong Kong
  cRegionASNoEa1 = 'asia-northeast1';         // Tokyo
  cRegionASNoEa2 = 'asia-northeast2';         // Osaka
  cRegionASSout1 = 'asia-south1';             // Mumbai
  cRegionASSoEa1 = 'asia-southeast1';         // Singapore
  cRegionASSoEa2 = 'asia-southeast2';         // Jakarta
  cRegionASSoEa3 = 'asia-southeast3';         // Seoul

Suppose you have the following function addMessage deployed to Firebase.

exports.addMessage = functions.https.onCall(async (data, context) => {
  const text = data.text;
  console.log('addMessage called with Text=' + text);
  // add your FB function and return the results as JSON object
  return { result: 'ok' };
});
  TOnFunctionSuccess = procedure(const Info: string; ResultObj: TJSONObject) of
    object;
  IFirebaseFunctions = interface(IInterface)
    procedure CallFunction(OnSuccess: TOnFunctionSuccess;
      OnRequestError: TOnRequestError; const FunctionName: string;
      Params: TJSONObject = nil);
    function CallFunctionSynchronous(const FunctionName: string;
      Params: TJSONObject = nil): TJSONObject;
  end;

You can call this function from your application with the following wrapper code:

function AddMessageWrapper(const Msg: string): string;
var
  Params, ResObj: TJSONObject;
begin
  Params := TJSONObject.Create;
  Params.AddPair('text', Msg);
  ResObj := IFirebaseConfiguration.Functions.CallFunctionSynchronous('addMessage', Params);
  result := ResObj.GetValue<string>('res');
end;

Warning: Note that JavaScript is a case-sensitive language. Therefore, write the function name and input parameters incorrect uppercase and lowercase letters!

Clone this wiki locally