Created
August 24, 2022 06:59
-
-
Save GitHub30/f1fb46b12dc13374449e1c1b6aa2ad0d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
# pip install google-cloud-firestore | |
from google.cloud import firestore | |
import os | |
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './push14-firebase-adminsdk-vgfzy-e8d93f3c32.json' | |
db = firestore.Client() | |
# Create an Event for notifying main thread. | |
callback_done = threading.Event() | |
first = False | |
import time | |
# Create a callback on_snapshot function to capture changes | |
def on_snapshot(doc_snapshot, changes, read_time): | |
for doc in doc_snapshot: | |
print(f'Received document snapshot: {doc.id}', changes, read_time, time.time()) | |
global first | |
if first: | |
callback_done.set() | |
first = True | |
doc_ref = db.collection(u'cities').document(u'SF') | |
# latency: 55ms | |
# db.collection('cities').doc('SF').set({abc: Date.now()}) | |
# Watch the document | |
doc_watch = doc_ref.on_snapshot(on_snapshot) | |
# Wait for the callback. | |
callback_done.wait() | |
# [START firestore_listen_detach] | |
# Terminate watch on a document | |
doc_watch.unsubscribe() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment