Cloud functions are rather… different. They work with pretty much any platform and that’s because they’re executed on the cloud.

That does however make it easy to be confused when getting started with it. I sure was. Here we’ll simplify it down as much as possible so you can start navigating your way around cloud functions yourself (the ecosystem is huge).

Protip: Much of this is playing around with the command line. If you’re using Android Studio, you can use the built-in terminal for the whole thing.

Install the Node.js

Many of you probably already have this installed. You guys can skip ahead. Otherwise, you’ll need Node.js to write the functions, as well as npm (which comes with Node.js) to install the FirebaseCLI in the next step.

Install the FirebaseCLI

npm install -g firebase-tools

With your command line/terminal, use npm to install the FirebaseCLI which should then give you access to commands you’ll need to start creating functions. (For the newbies, it doesn’t matter what directory you’re currently on. The -g installs the CLI globally).

Login and Init Functions

  • Run firebase login and login to your account (with your Firebase project).
  • Create a folder called functions in your app’s root folder and navigate/open your command line there.
  • Run firebase init functions and follow the instructions.

After doing this, inside your functions folder, you should see a file called index.js (or .ts if you chose typescript). This is where your functions will be written.

Writing a Function..?

Okay, actually writing the functions could be a whole series in itself. There are countless triggers to use and countless methods to execute. Let me know if you want me to make this series.

// Adds a message that welcomes new users into the chat.
exports.addWelcomeMessages = functions.auth.user().onCreate(async (user) => {
  console.log('A new user signed in for the first time.');
  const fullName = user.displayName || 'Anonymous';

  // Saves the new welcome message into the database
  // which then displays it in the FriendlyChat clients.
  await admin.database().ref('messages').push({
    name: 'Firebase Bot',
    profilePicUrl: '/images/firebase-logo.png', // Firebase logo
    text: `${fullName} signed in for the first time! Welcome!`,
  console.log('Welcome message written to database.');

For now, this is a simple function you can paste directly into your index.js file. Whenever a user logs in for the first time, a welcome message is written to the Realtime Database.

Deploying your Function

firebase deploy --only functions

Back in your terminal outside your functions folder, run this command and your functions should then be active on the cloud. In this case (from the code above), you have a function that triggers whenever a user logs in for the first time. Then you can test your app by just creating a user to sign-in for the first time.


That’s a basic introduction to Firebase Cloud Functions. If you want me to make a series on actually writing the functions, let me know in the comments!


Subscribe to the Newsletter