admin管理员组

文章数量:1291102

I am running a Firebase Emulator with a Realtime Database and trying to create a listener in Python that detects changes inside the database. My Code:

    import os
    import firebase_admin
    from firebase_admin import credentials, db
    
    def listener(event):
        print(f"Received event: {event.path} - data: {event.data}") # Output only once at the beginning: Received event: / - data: None
    
    if __name__ == '__main__':
        os.environ["FIREBASE_DATABASE_EMULATOR_HOST"] = "<IP-ADDRESS>:9000"
        
        cred = credentials.Certificate('serviceAccountKey.json')
        firebase_admin.initialize_app(cred, {"databaseURL": "http://<IP-ADDRESS>:9000/?ns=<project-id>" })
    
        ref = db.reference('my_child')
    
        print(ref.path)  # Prints the correct path
        print(ref.get())  # Successfully retrieves the correct JSON data
    
        ref.listen(listener)
        while True:
           time.sleep(10)

The listener is only triggered once at the start (with event.data = None), but it does not respond to further changes in the database. When I Call ref.get(), I can correctly retrieve the data, but the listener function is never triggered again.

Has anyone successfully used listen() with the emulator? Do I need any additional configuration to make listen() work properly?

本文标签: Firebase Realtime Database EmulatorPython Listener Not Triggering on Data ChangesStack Overflow