Instantly share code, notes, and snippets.

Embed
What would you like to do?
example of listening firestore realtime update
package main
import (
"firebase.google.com/go"
"cloud.google.com/go/firestore"
"context"
"log"
"math/rand"
"fmt"
)
var client *firestore.Client
var ctx = context.Background()
func main(){
conf := &firebase.Config{ProjectID: "GCP_PROJECT_ID"}
app, err := firebase.NewApp(ctx, conf)
if err != nil {
log.Fatalln(err)
}
client, err = app.Firestore(ctx)
if err != nil {
log.Fatalln(err)
}
defer client.Close()
snapshotIterator := client.Collection("users").Doc("user1").Snapshots(ctx)
defer snapshotIterator.Stop()
for{
data, err := snapshotIterator.Next()
if err != nil {
log.Fatalln(err)
}
fmt.Printf("data exist: %+v, data: %+v\n", data.Exists(), data.Data())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment