Firebase Admin SDK Event Listener not triggered

199

Turned out there was no problem regarding the Go code. The problem was the way I updated the Realtime Database from Flutter.

This is what I used to perform updates:

await ref.update({"value": value});

What fixed the problem was to use "set" instead of "update" the following way:

await ref.child("value").set(value);

This way my Go-Listener method then was finally triggered and everything was working as expected.

Share:
199
juicy
Author by

juicy

Updated on January 01, 2023

Comments

  • juicy
    juicy 10 months

    I am using the Go Firebase Admin SDK and listening to changes in the Realtime Database. The problem is that the listener is ONLY triggered if I manually update the data from the Firebase Console, if I change data from another app (in this case Flutter), the listener is NOT triggered even though the changes can be seen in the Firebase Console (so the data definitely changed).

    I even tried performing an update via Firebase Database REST API https://firebase.google.com/docs/reference/rest/database#section-streaming
    Which had the same result: Data changes are viewable in the Console, but still don't trigger the listener.

    Here the way I'm listening to changes in Go:

    func listenToFirebase(ref *db.Ref, ctx context.Context) {
        iter, err := ref.Listen(ctx)
        if err != nil {
            fmt.Printf(" Error: failed to create Listener %v\n", err)
            return
        }
        defer iter.Stop()
    
        for {
            if iter.Done() {
                break
            }
            event, err := iter.Next()
            if err != nil {
                // Handle error here based on specific usecase
                // We can continue Listening
                log.Printf("%v\n", err)
                continue
            }
            fmt.Printf("Listener | Ref Path: %s | event.Path %s | event.Snapshot() = %v\n", ref.Path, event.Path, event.Snapshot())
            fmt.Printf("\n")
        }
    }
    

    The fact that the listener is triggered by updating data from the Console, indicates that the listener is working properly.

    P.S.: The Listen-Method has not yet been integrated to the Go Firebase Admin SDK and is from https://github.com/firebase/firebase-admin-go/issues/229