googleads
Store and sync data in Firebase with Golang | Agira Technologies
Golang Standard Technical

Store and sync data in Firebase with Golang

Here, I am going to describe how to store and sync data in Firebase with Golang. Aslo, I have explained the how to connect the Firebase with Golang and authenticate using two types of credentials, namely Json and OAuth. When I first tried this, I faced some issues during connectivity, and hence thought it would be good to consolidate the process into an article.
The Firebase Real time Database, by definition, is a cloud-hosted database. This Firebase NoSQL cloud database is trending as the new go-to data storage concept. With this, data is stored as JSON, and not in the traditional format. Also, Firebase performs real time data synchronisation to every client that is connected.

How does it work?

The Firebase Real time Database lets you build rich, collaborative applications by allowing secure access to the database directly from client-side code.

Since I am describing how to implement a client of Firebase with Golang, first, we need to install the Firego package, and the fireauth package for authentication. Below are the steps:

Installation

 

If you want to add authentication token (as mentioned above), install the fireauth package also

 

If you want to add OAuth authentication by means of JWT (JSON Web Token):

 

Usage

Next, in order to use Firebase, we need to access the installed packages, as below:

import firego

 

Next, we need to generate a new firego reference:

 

If we have an already existing http client, we can generate the reference as below:

 

Request Timeouts

By default, the Firebase reference will timeout after 30 seconds of trying to reach a Firebase server. You can configure this value by setting the global timeout duration, as:

 

Auth Tokens

Now, in order to set the authentication for the Firebase, we need to get the token using fireauth package.

Get the secret key from under Project Settings. The url should looks something like:

“https://console.firebase.google.com/project/YOUR_PROJECT_NAME/settings/database” or “https://console.firebase.google.com/project/YOUR_PROJECT_NAME/settings/serviceaccounts/databasesecrets”

 

If you don’t want authentication:

 

Are you going to use OAuth authentication?
Then, follow as below:

 

Once the authentication is done, and all has been set, we can access the set database. Let us see them below:

Get Value

Get the values of the reference.

 

Querying

Take a look at Firebase’s query parameters for more information on what each function does.

 

Set Value

Set the value of the reference.

 

Push Value

Push creates a reference to an auto-generated child location.

 

Update Child

Update the specific child with the given value.

 

Remove Value

Remove the reference from the cloud.

 

Watch a Node

The Watch function listens for any changes on an instance of the specified Firebase. The result is passed over to the given channel. Since only one connection can be established at any given time, f.StopWatching should be mandatorily issued before the next call to Watch. If not, the second or next call will result in the given channel being closed and nil being returned immediately.

 

Change reference

You, by now, know that a reference is used to save or read data from a specified reference. Ref function returns a copy of an existing Firebase reference with a new path.

 

That’s it. We now have a basic idea as to how to implement a Firebase with Golang, and how to perform the basic operations on the same. As mentioned in the beginning, two options are given here for authentication on Firebase with Golang. It is simply a matter of choice.

The above example is given in detail here. You can try it out.

Also, you can check the GoDocs for more details.

The following two tabs change content below.

Reddy Sai

Young Senior Software Developer having 2+years of experience in Web development, skilled in Golang, Ruby, AngularJS, Ruby on Rails & NodeJS. With handful of skills he keep thriving through all the Edge of Web development to become a perfect full stack Golang Expert. Besides, this young techie will always buzz up & surprise us with his beautiful Music collections.

Leave a Reply

Your email address will not be published. Required fields are marked *