Firebase: Saving scores into Firestore with Go Functions

Philippe Martin
4 min readJan 19, 2019

--

We will explore in this article how to use the Go language to access the Firestore database in Firebase when using GCP Functions.

As an example, we will experiment how to save scores from a game.

You can get the sources at https://github.com/feloy/firestore-score

The first step is to create a new project into Firebase, then to create a new Firestore database.

Saving the score

We can now create a new collection, named Score that will receive the information of a new score from your game.

An we will store in this collection these different fields. All this information will be sent by your application when a user finishes a game:

  • uid: the unique identifier of the user sending the score,
  • name: the user name,
  • points: the number of points for the score,
  • details: some details about the score,
  • country: the country of the user.

As an example, we can create a new score into the collection:

Here is some code that you could use to send the information from an Android app written in Java:

Computing the range

We now want to compute the range of this new score, to be able to send back to the user its range for this game, and also to be able to display a hall of fame.

For this, we will create a new Function that will be triggered each time a Score is added and that will compute this range.

As this time, to work with Functions written in Go, you have to go to the Google Cloud Platform console and select your Firebase project, then select the Compute > Cloud Functions menu entry, and click the “Create function” button.

On the form, select the Cloud Firestore trigger, the create event type and the Go runtime. Set Score/{id} as the document path and leave the content of the function as is, for the moment.

As a first test, you can go back to the Firebase console and select the Develop > Functions menu entry. The function you just created should appear in the list of functions.

Now select the Develop > Database menu entry, and create a new entry into the Score collection, with a name field.

Let’s go back to the Functions dashboard and select the Logs tab. You should see some logs appear that indicate your function has been triggered:

You could continue to edit your function from the GCP console, or you can work on your function from your local computer, and publish it with the gcloud command.

Let’s create a new newScore.go file, with the default content given by the create form precedently, a go.mod file then a publish.sh script to be run to publish your function:

Getting the values from the document

The e parameter of the function, of type FirestoreEvent, contains the value of the document just created. We can first create some helper functions to extract the values from this parameter, and then fill a structure that represents the Score collection:

Storing this score in a Points collection

To be able to compute efficiently the rank of this score, we will create a second collection Points which will contain a list of documents, each document containing the list of scores for a given number of points.

For example, if the first player sends a score with 124 points, an entry will be added in the Points collection, containing a copy of this score. When another player sends a score with 124 points, a copy of this score will be added to the previous Points document.

Finally, the function will add the rank computed for this Score into the Score document created by the player.

--

--