Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a64e4af

Browse filesBrowse files
frankynkurtisvg
authored andcommitted
Firestore watch samples (GoogleCloudPlatform#1883)
* Add base code for realtime listener snippets * Updating samples based on fixes * Update requires to point to latest release
1 parent 412b61f commit a64e4af
Copy full SHA for a64e4af

File tree

Expand file treeCollapse file tree

3 files changed

+131
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+131
-1
lines changed
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
google-cloud-firestore==0.30.0
1+
google-cloud-firestore==0.31.0

‎firestore/cloud-client/snippets.py

Copy file name to clipboardExpand all lines: firestore/cloud-client/snippets.py
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# limitations under the License.
1313

1414
import datetime
15+
from time import sleep
1516

1617
from google.cloud import firestore
1718
import google.cloud.exceptions
@@ -603,6 +604,114 @@ def cursor_paginate():
603604
return next_query
604605

605606

607+
def listen_document():
608+
db = firestore.Client()
609+
# [START listen_document]
610+
611+
# Create a callback on_snapshot function to capture changes
612+
def on_snapshot(doc_snapshot, changes, read_time):
613+
for doc in doc_snapshot:
614+
print(u'Received document snapshot: {}'.format(doc.id))
615+
616+
doc_ref = db.collection(u'cities').document(u'SF')
617+
618+
# Watch the document
619+
doc_watch = doc_ref.on_snapshot(on_snapshot)
620+
# [END listen_document]
621+
622+
# Creating document
623+
data = {
624+
u'name': u'San Francisco',
625+
u'state': u'CA',
626+
u'country': u'USA',
627+
u'capital': False,
628+
u'population': 860000
629+
}
630+
doc_ref.set(data)
631+
sleep(3)
632+
# [START detach_listener]
633+
# Terminate watch on a document
634+
doc_watch.unsubscribe()
635+
# [START detach_listener]
636+
637+
638+
def listen_multiple():
639+
db = firestore.Client()
640+
# [START listen_multiple]
641+
642+
# Create a callback on_snapshot function to capture changes
643+
def on_snapshot(col_snapshot, changes, read_time):
644+
print(u'Callback received query snapshot.')
645+
print(u'Current cities in California:')
646+
for doc in col_snapshot:
647+
print(u'{}'.format(doc.id))
648+
649+
col_query = db.collection(u'cities').where(u'state', u'==', u'CA')
650+
651+
# Watch the collection query
652+
query_watch = col_query.on_snapshot(on_snapshot)
653+
654+
# [END listen_multiple]
655+
# Creating document
656+
data = {
657+
u'name': u'San Francisco',
658+
u'state': u'CA',
659+
u'country': u'USA',
660+
u'capital': False,
661+
u'population': 860000
662+
}
663+
db.collection(u'cities').document(u'SF').set(data)
664+
sleep(1)
665+
query_watch.unsubscribe()
666+
667+
668+
def listen_for_changes():
669+
db = firestore.Client()
670+
# [START listen_for_changes]
671+
672+
# Create a callback on_snapshot function to capture changes
673+
def on_snapshot(col_snapshot, changes, read_time):
674+
print(u'Callback received query snapshot.')
675+
print(u'Current cities in California: ')
676+
for change in changes:
677+
if change.type.name == "ADDED":
678+
print(u'New city: {}'.format(change.document.id))
679+
elif change.type.name == "MODIFIED":
680+
print(u'Modified city: {}'.format(change.document.id))
681+
elif change.type.name == "REMOVED":
682+
print(u'Removed city: {}'.format(change.document.id))
683+
684+
col_query = db.collection(u'cities').where(u'state', u'==', u'CA')
685+
686+
# Watch the collection query
687+
query_watch = col_query.on_snapshot(on_snapshot)
688+
689+
# [END listen_for_changes]
690+
mtv_document = db.collection(u'cities').document(u'MTV')
691+
# Creating document
692+
mtv_document.set({
693+
u'name': u'Mountain View',
694+
u'state': u'CA',
695+
u'country': u'USA',
696+
u'capital': False,
697+
u'population': 80000
698+
})
699+
700+
# Modifying document
701+
mtv_document.update({
702+
u'name': u'Mountain View',
703+
u'state': u'CA',
704+
u'country': u'USA',
705+
u'capital': False,
706+
u'population': 90000
707+
})
708+
709+
# Delete document
710+
mtv_document.delete()
711+
sleep(1)
712+
query_watch.unsubscribe()
713+
714+
606715
def cursor_multiple_conditions():
607716
db = firestore.Client()
608717
# [START cursor_multiple_conditions]

‎firestore/cloud-client/snippets_test.py

Copy file name to clipboardExpand all lines: firestore/cloud-client/snippets_test.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,26 @@ def test_delete_field(db):
220220
snippets.delete_field()
221221

222222

223+
def test_listen_document(capsys):
224+
snippets.listen_document()
225+
out, _ = capsys.readouterr()
226+
assert "Received document snapshot: SF" in out
227+
228+
229+
def test_listen_multiple(capsys):
230+
snippets.listen_multiple()
231+
out, _ = capsys.readouterr()
232+
assert "Current cities in California:" in out
233+
assert "SF" in out
234+
235+
236+
def test_listen_for_changes(capsys):
237+
snippets.listen_for_changes()
238+
out, _ = capsys.readouterr()
239+
assert "New city: MTV" in out
240+
assert "Modified city: MTV" in out
241+
assert "Removed city: MTV" in out
242+
243+
223244
def test_delete_full_collection():
224245
snippets.delete_full_collection()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.