|
| 1 | +// Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + "os" |
| 21 | + |
| 22 | + "golang.org/x/net/context" |
| 23 | + "google.golang.org/api/option" |
| 24 | + |
| 25 | + firebase "firebase.google.com/go" |
| 26 | + "firebase.google.com/go/db" |
| 27 | +) |
| 28 | + |
| 29 | +// Key is a json-serializable type. |
| 30 | +type Key struct { |
| 31 | + Key1 string `json:"key1"` |
| 32 | +} |
| 33 | + |
| 34 | +func main() { |
| 35 | + |
| 36 | + // opt := option.WithCredentialsFile("c:/users/username/.firebase/firebase.json") // Windows |
| 37 | + // |
| 38 | + opt := option.WithCredentialsFile("/home/username/.firebase/firebase.json") // Linux, edit 1. |
| 39 | + |
| 40 | + config := &firebase.Config{ |
| 41 | + DatabaseURL: "https://databaseName.firebaseio.com", // edit 2. |
| 42 | + } |
| 43 | + |
| 44 | + app, err := firebase.NewApp(context.Background(), config, opt) |
| 45 | + if err != nil { |
| 46 | + log.Fatal(err) |
| 47 | + } |
| 48 | + |
| 49 | + ctx := context.Background() |
| 50 | + |
| 51 | + // DatabaseWithURL |
| 52 | + client, err := app.Database(ctx) |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + log.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + // https://firebase.google.com/docs/reference/js/firebase.database.Reference.html#key |
| 59 | + // |
| 60 | + // key = The last part of the Reference's path. |
| 61 | + |
| 62 | + testpath := "user1/path1" |
| 63 | + ref := client.NewRef(testpath) |
| 64 | + |
| 65 | + args := os.Args |
| 66 | + if len(args) > 1 { |
| 67 | + triggerEvent(ctx, client, testpath, args[1]) |
| 68 | + return // exit app |
| 69 | + } |
| 70 | + |
| 71 | + // SnapshotIterator |
| 72 | + iter, err := ref.Listen(ctx) |
| 73 | + if err != nil { |
| 74 | + fmt.Printf(" Error: failed to create Listener %v\n", err) |
| 75 | + return // exit app |
| 76 | + } |
| 77 | + |
| 78 | + fmt.Printf("Initial snapshots:\n") |
| 79 | + |
| 80 | + fmt.Printf("1st Listener | Ref Path: %s | iter.Snapshot = %v\n", ref.Path, iter.Snapshot) |
| 81 | + fmt.Printf(" | Ref Key: %s \n", ref.Key) |
| 82 | + |
| 83 | + defer iter.Stop() |
| 84 | + |
| 85 | + var key Key |
| 86 | + |
| 87 | + go func() { |
| 88 | + for { |
| 89 | + event, err := iter.Next() |
| 90 | + |
| 91 | + if err != nil { |
| 92 | + break |
| 93 | + } |
| 94 | + |
| 95 | + err = event.Unmarshal(&key) |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + fmt.Printf("1st Listener | Error: Unmarshal %v\n", err) |
| 99 | + } else { |
| 100 | + fmt.Printf("1st Listener | Ref Path: %s | event.Path %s | event.Unmarshal(&key) key.Key1 = %s\n", ref.Path, event.Path, key.Key1) |
| 101 | + fmt.Printf("1st Listener | Ref Path: %s | event.Path %s | event.Unmarshal(&key) key = %v\n", ref.Path, event.Path, key) |
| 102 | + } |
| 103 | + |
| 104 | + fmt.Printf("1st Listener | Ref Path: %s | event.Path %s | event.Snapshot() = %v\n", ref.Path, event.Path, event.Snapshot()) |
| 105 | + fmt.Printf("\n") |
| 106 | + } |
| 107 | + }() |
| 108 | + |
| 109 | + // 2nd listener |
| 110 | + testpath2 := "user1/path1/path2" |
| 111 | + ref2 := client.NewRef(testpath2) |
| 112 | + |
| 113 | + iter2, err := ref2.Listen(ctx) |
| 114 | + if err != nil { |
| 115 | + fmt.Printf(" Error: failed to create Listener %v\n", err) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + fmt.Printf("2nd Listener | Ref Path: %s | iter.Snapshot = %v\n", ref2.Path, iter2.Snapshot) |
| 120 | + fmt.Printf(" | Ref Key: %s \n", ref2.Key) |
| 121 | + |
| 122 | + defer iter2.Stop() |
| 123 | + |
| 124 | + go func() { |
| 125 | + for { |
| 126 | + event, err := iter2.Next() |
| 127 | + |
| 128 | + if err != nil { |
| 129 | + break |
| 130 | + } |
| 131 | + |
| 132 | + fmt.Printf("2nd Listener | Ref Path: %s | event.Path %s | event.Snapshot() = %v\n", ref2.Path, event.Path, event.Snapshot()) |
| 133 | + fmt.Printf("\n") |
| 134 | + } |
| 135 | + }() |
| 136 | + |
| 137 | + fmt.Printf("\n >>> open a new separate command line terminal, to trigger events, run: go run . anyvalue\n") |
| 138 | + fmt.Printf("\n >>> OR edit value of any key from %s in firebase console to trigger events\n\n", testpath) |
| 139 | + fmt.Printf("\n >>> press <enter> to close http connection\n\n") |
| 140 | + fmt.Printf("Waiting for events...\n\n") |
| 141 | + |
| 142 | + fmt.Scanln() |
| 143 | + iter.Stop() |
| 144 | + |
| 145 | + fmt.Printf("\n >>> press <enter> to exit app\n\n\n") |
| 146 | + fmt.Scanln() |
| 147 | +} |
| 148 | + |
| 149 | +func triggerEvent(ctx context.Context, client *db.Client, testpath string, val string) { |
| 150 | + |
| 151 | + var key Key |
| 152 | + |
| 153 | + key.Key1 = val |
| 154 | + |
| 155 | + ref := client.NewRef(testpath + "/path2/path3") |
| 156 | + |
| 157 | + if err := ref.Set(ctx, key); err != nil { |
| 158 | + log.Fatal(err) |
| 159 | + } else { |
| 160 | + fmt.Printf("OK - Set %s to key.Key1=%v\n", testpath+"/path2/path3", val) |
| 161 | + } |
| 162 | +} |
0 commit comments