How to detect realtime listener errors in firebase firestore database?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC H Dvoks String Quartet No 12 Ame
--
Chapters
00:00 Question
03:11 Accepted answer (Score 9)
04:24 Thank you
--
Full question
https://stackoverflow.com/questions/5587...
Question links:
[listeners]: https://firebase.google.com/docs/firesto...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #firebase #googlecloudfirestore
#avk47
--
Track title: CC H Dvoks String Quartet No 12 Ame
--
Chapters
00:00 Question
03:11 Accepted answer (Score 9)
04:24 Thank you
--
Full question
https://stackoverflow.com/questions/5587...
Question links:
[listeners]: https://firebase.google.com/docs/firesto...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #firebase #googlecloudfirestore
#avk47
ACCEPTED ANSWER
Score 9
I figured out an alternative method to detecting the listener error and restarting the listener after a server side close. I have no idea how to catch the actual error, but I figured out how to detect when Firestore just randomly closes the listener connection.
In the Firebase listener code they keep track of a private variable '_closed' that becomes true if the connection ever gets closed for any reason. Therefore, if we periodically check that, we can restart our listener and be on our merry way.
Using the code from before, I added a new method start_snapshot in order to restart our failed listener expression on error, and in my long running code, I added a check against the listener to see if it is closed, and restart it if it is.
class TestWatchInfo():
def __init__(self):
self.start_snapshot()
def start_snapshot(self):
self.query_watch = db.collection(u'info').on_snapshot(self.on_snapshot)
def on_snapshot(self, col_snapshot, changes, read_time):
try:
for change in changes:
pass
except Exception as err:
print(err)
print("Error occurred at " + str(time.ctime()))
traceback.print_exc()
if __name__ == '__main__':
try:
test_object = TestWatchInfo()
while(True):
if test_object.query_watch._closed:
test_object.start_snapshot()
# code here
except Exception as err:
print(err)
print("Error occurred at " + str(time.ctime()))
traceback.print_exc()