Since Golang is considering a lightweight programming language and Firebase is the next-generation Backend-as-a-Service, we think it could be a savvy combination to develop with ease both performant and secure microservices or stand-alone projects. Here are several advantages that could convince you to try this combo.
When it comes to Golang, it is as speedy as a compiled language, but it feels like an interpreted one. Moreover, as we mentioned in one of our previous articles, it has clean syntax with text-based workflow and clear language specification. Thus, you can write the code fast, and even faster compilation speeds are allowed for a rapid feedback style.
On the other hand, Firebase is an all-encompassing product that has everything you need to develop. From a real database to authentication modules. when you connect your app with Firebase, you’re not connecting through normal HTTP, but through a WebSocket which is much faster than HTTP and sends you new data as soon as it’s updated.
Firebase Storage provides a simple way to save binary files to Google Cloud Storage directly from the client thanks to its own system of security rules to protect your GCloud Socket from the masses while granting detailed write privileges to your authenticated clients.
Regarding Authentication, Firebase Auth has built in email/password authentication system. It also supports OAuth2 for Google, Facebook, Twitter and GitHub. Moreover, Firebase Auth integrates directly into Firebase Database so you can use it to control access to your data.
Last but not least, Firebase includes an easy-to-use hosting services for all your static files that serve them from a global CDN with HTTP/2 making your development particularly painless.
Firebase admin SDK for Golang
The Firebase Admin Go SDK enables access to Firebase services from privileged environments (such as servers or cloud) in Go. Currently, this SDK provides Firebase custom authentication support.
To install Firebase Admin Go SDK, simply execute the following command in a terminal from your $GOPATH:
go get firebase.google.com/go
Firebase Admin SDKs enable application developers to programmatically access Firebase services from trusted environments. They complement the Firebase client SDKs, which enable end users to access Firebase from their web browsers and mobile devices.
And that’s only the beginning. To explore further, here are 4 Go libraries and apps for Firebase that we think you will find useful:
Go-fcm – Firebase Cloud Messaging (FCM) Library using Golang (Go). This library uses HTTP/JSON Firebase Cloud Messaging connection server protocol. Its main features are: send messages to a topic, send messages to a device list, message can be a notification or data payload, supports condition attribute (fcm only), instance Id Features
Examples:
Send to A topic
package main
import (
"fmt"
"github.com/NaySoftware/go-fcm"
)
const (
serverKey = "YOUR-KEY"
topic = "/topics/someTopic"
)
func main() {
data := map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
c := fcm.NewFcmClient(serverKey)
c.NewFcmMsgTo(topic, data)
status, err := c.Send()
if err == nil {
status.PrintResults()
} else {
fmt.Println(err)
}
}
Send to a list of Devices (tokens)
package main
import (
"fmt"
"github.com/NaySoftware/go-fcm"
)
const (
serverKey = "YOUR-KEY"
)
func main() {
data := map[string]string{
"msg": "Hello World1",
"sum": "Happy Day",
}
ids := []string{
"token1",
}
xds := []string{
"token5",
"token6",
"token7",
}
c := fcm.NewFcmClient(serverKey)
c.NewFcmRegIdsMsg(ids, data)
c.AppendDevices(xds)
status, err := c.Send()
if err == nil {
status.PrintResults()
} else {
fmt.Println(err)
}
}
Firebase-server-sdk-go – This is the Server SDK written in Golang for the newly announced Firebase suite of services. This SDK, like its Java and Node counterparts, supports the following functions needed on the application server: Authentication, Real-time Database, Cloud Messaging (FCM)
Examples:
Create Custom Tokens
To create a custom token, pass the unique user ID used by your auth system to the CreateCustomToken() method:
auth, _ := firebase.GetAuth()
token, err := auth.CreateCustomToken(userId, nil)
You can also optionally specify additional claims to be included in the custom token. These claims will be available in the auth/request.auth
objects in your Security Rules. For example:
auth, _ := firebase.GetAuth()
developerClaims = make(firebase.Claims)
developerClaims["premium_account"] = true
token, err := auth.CreateCustomToken(userId, &developerClaims)
Verify ID Tokens
To verify and decode an ID Token with the SDK, pass the ID Token to the VerifyIDToken method. If the ID Token is not expired and is properly signed, the method decodes the ID Token.
auth, _ := firebase.GetAuth()
decodedToken, err := auth.VerifyIDToken(idTokenString)
if err == nil {
uid, found := decodedToken.Uid()
}
Fireauth – A Firebase token generator written in Go
cosn/firebase – This a library for invoking the Firebase REST API.
In conclusion, a combination of those two can lead you to solid and scalable application development. Golang might be the perfect solution to scale your idea, while Firebase has all that you could need to begin a worthwhile business solution or to launch your current configuration forward on the right track.