{"id":3196,"date":"2018-02-27T08:11:15","date_gmt":"2018-02-27T08:11:15","guid":{"rendered":"https:\/\/intelligentbee.com\/blog\/?p=3196"},"modified":"2025-03-31T07:45:00","modified_gmt":"2025-03-31T07:45:00","slug":"firebase-integration-golang","status":"publish","type":"post","link":"https:\/\/intelligentbee.com\/blog\/firebase-integration-golang\/","title":{"rendered":"Firebase integration for Golang"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">Since Golang is considering a lightweight programming language and <\/span><span style=\"font-weight: 400;\">Firebase is the<\/span><span style=\"font-weight: 400;\"> next-generation Backend-as-a-Service, we think it could be a savvy combination to develop with ease <\/span><span style=\"font-weight: 400;\">both performant and secure microservices or stand-alone projects<\/span><span style=\"font-weight: 400;\">. Here are several advantages that could convince you to try this combo.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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 <\/span><span style=\"font-weight: 400;\">clean syntax with text-based workflow<\/span><span style=\"font-weight: 400;\"> and clear language specification. Thus, you can write the code fast, and even faster compilation speeds are allowed for a rapid feedback style.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">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\u2019re not connecting through normal HTTP, but through a WebSocket which is much faster than HTTP and sends you new data as soon as it\u2019s updated. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">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. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">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. <\/span><\/p>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<h2><span style=\"font-weight: 400;\">Firebase admin SDK for Golang<\/span><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To install Firebase Admin Go SDK, simply execute the following command in a terminal from your $GOPATH:<\/span><\/p>\n<pre class=\"\"><code>go get firebase.google.com\/go<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">And that\u2019s only the beginning. To explore further, here are 4 Go libraries and apps for Firebase that we think you will find useful:<\/span><\/p>\n<p><a href=\"https:\/\/github.com\/NaySoftware\/go-fcm\" target=\"_blank\" rel=\"noopener\"><b>Go-fcm<\/b><\/a><b> &#8211; <\/b><span style=\"font-weight: 400;\">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<\/span><\/p>\n<h3>Examples:<\/h3>\n<div class=\"highlight highlight-source-go\">\n<h4>Send to A topic<\/h4>\n<div class=\"highlight highlight-source-go\">\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n\t\"fmt\"\r\n    \"github.com\/NaySoftware\/go-fcm\"\r\n)\r\n\r\nconst (\r\n\t serverKey = \"YOUR-KEY\"\r\n     topic = \"\/topics\/someTopic\"\r\n)\r\n\r\nfunc main() {\r\n\r\n\tdata := map[string]string{\r\n\t\t\"msg\": \"Hello World1\",\r\n\t\t\"sum\": \"Happy Day\",\r\n\t}\r\n\r\n\tc := fcm.NewFcmClient(serverKey)\r\n\tc.NewFcmMsgTo(topic, data)\r\n\r\n\r\n\tstatus, err := c.Send()\r\n\r\n\r\n\tif err == nil {\r\n    status.PrintResults()\r\n\t} else {\r\n\t\tfmt.Println(err)\r\n\t}\r\n\r\n}<\/pre>\n<\/div>\n<h4><a id=\"user-content-send-to-a-list-of-devices-tokens\" class=\"anchor\" href=\"https:\/\/github.com\/NaySoftware\/go-fcm#send-to-a-list-of-devices-tokens\" aria-hidden=\"true\"><\/a>Send to a list of Devices (tokens)<\/h4>\n<div class=\"highlight highlight-source-go\">\n<pre class=\"lang:default decode:true \">package main\r\n\r\nimport (\r\n\t\"fmt\"\r\n    \"github.com\/NaySoftware\/go-fcm\"\r\n)\r\n\r\nconst (\r\n\t serverKey = \"YOUR-KEY\"\r\n)\r\n\r\nfunc main() {\r\n\r\n\tdata := map[string]string{\r\n\t\t\"msg\": \"Hello World1\",\r\n\t\t\"sum\": \"Happy Day\",\r\n\t}\r\n\r\n  ids := []string{\r\n      \"token1\",\r\n  }\r\n\r\n\r\n  xds := []string{\r\n      \"token5\",\r\n      \"token6\",\r\n      \"token7\",\r\n  }\r\n\r\n\tc := fcm.NewFcmClient(serverKey)\r\n    c.NewFcmRegIdsMsg(ids, data)\r\n    c.AppendDevices(xds)\r\n\r\n\tstatus, err := c.Send()\r\n\r\n\r\n\tif err == nil {\r\n    status.PrintResults()\r\n\t} else {\r\n\t\tfmt.Println(err)\r\n\t}\r\n\r\n}<\/pre>\n<p><a href=\"https:\/\/github.com\/wuman\/firebase-server-sdk-go\" target=\"_blank\" rel=\"noopener\"><b>Firebase-server-sdk-go<\/b><\/a><b> &#8211; <\/b><span style=\"font-weight: 400;\">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:<\/span><b> Authentication<\/b><span style=\"font-weight: 400;\">, <\/span><b>Real-time Database<\/b><span style=\"font-weight: 400;\">, <\/span><b>Cloud Messaging (FCM)<\/b><\/p>\n<h3>Examples:<\/h3>\n<h4>Create Custom Tokens<\/h4>\n<\/div>\n<p><span style=\"font-weight: 400;\">To create a custom token, pass the unique user ID used by your auth system to the CreateCustomToken() method:<\/span><\/p>\n<pre><code>auth, _ := firebase.GetAuth()\r\ntoken, err := auth.CreateCustomToken(userId, nil)\r\n<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">You can also optionally specify additional claims to be included in the custom token. These claims will be available in the\u00a0<\/span><code>auth\/request.auth<\/code>\u00a0<span style=\"font-weight: 400;\">objects\u00a0in your Security Rules. For example:<\/span><\/p>\n<pre><code>auth, _ := firebase.GetAuth()\r\ndeveloperClaims = make(firebase.Claims)\r\ndeveloperClaims[\"premium_account\"] = true\r\ntoken, err := auth.CreateCustomToken(userId, &amp;developerClaims)<\/code><\/pre>\n<h4>Verify ID Tokens<\/h4>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<pre class=\"\"><code>auth, _ := firebase.GetAuth()\r\ndecodedToken, err := auth.VerifyIDToken(idTokenString)\r\nif err == nil {\r\n\tuid, found := decodedToken.Uid()\r\n}<\/code><\/pre>\n<\/div>\n<p><a href=\"https:\/\/github.com\/zabawaba99\/fireauth\" target=\"_blank\" rel=\"noopener\"><b>Fireauth<\/b><\/a><b> &#8211; <\/b><span style=\"font-weight: 400;\">A Firebase token generator written in Go<\/span><\/p>\n<p><a href=\"https:\/\/github.com\/cosn\/firebase\" target=\"_blank\" rel=\"noopener\"><b>cosn\/firebase<\/b><\/a><span style=\"font-weight: 400;\"> &#8211; This a library for invoking the Firebase REST API.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In conclusion, <\/span><span style=\"font-weight: 400;\">a combination of those two can lead you to solid and scalable application development.<\/span> <span style=\"font-weight: 400;\">Golang might be the perfect solution to scale your idea, while Firebase <\/span><span style=\"font-weight: 400;\">has all that you could need to begin a worthwhile business solution or to launch your current configuration forward on the right track<\/span><span style=\"font-weight: 400;\">.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Golang is considering a lightweight programming language and Firebase is the next-generation Backend-as-a-Service, we think it could be a [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":3197,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73,78],"tags":[],"yst_prominent_words":[894,1039,902,901,900,899,898,897,896,895,281,893,892,891,890,889,888,887,886,412],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/3196"}],"collection":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/comments?post=3196"}],"version-history":[{"count":4,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/3196\/revisions"}],"predecessor-version":[{"id":133394,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/posts\/3196\/revisions\/133394"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media\/3197"}],"wp:attachment":[{"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/media?parent=3196"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/categories?post=3196"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/tags?post=3196"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/intelligentbee.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=3196"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}