GeezerVison is showing github.com — Firestore Realtime Listener Timing Out · Issue #2888 · googleapis/google-cloud-java
Firestore Realtime Listener Timing Out #2888DescriptionI'm trying to use Firestore in order to set up realtime listeners for a collection. Whenever a document is added, modified, or deleted in a collection, I want the listener to be called. My code is currently working for one collection, but when I try the same code on a larger collection, it fails with the error:
Listen failed: com.google.cloud.firestore.FirestoreException: Backend ended Listen stream: The datastore operation timed out, or the data was temporarily unavailable.
How should I deal with this issue in order to track changes in this collection? Can I manually set a higher time out threshold or use some other technique? My code implementation is below for reference:
/**
* Sets up a listener at the given collection reference. When changes are made in this collection, it writes a flat
* text file for import into backend.
* @param collectionReference The Collection Reference that we want to listen to for changes.
*/
public static void listenToCollection(CollectionReference collectionReference) {
AtomicBoolean initialUpdate = new AtomicBoolean(true);
System.out.println("Initializing listener for: " + collectionReference.getId());
collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirestoreException e) {
// Error Handling
if (e != null) {
System.err.println("Listen failed: " + e);
return;
}
// If this is the first time this function is called, it's simply reading everything in the collection
// We don't care about the initial value, only the updates, so we simply ignore the first call
if (initialUpdate.get()) {
initialUpdate.set(false);
System.out.println("Initial update complete...\nListener active for " + collectionReference.getId() + "...");
return;
}
// A document has changed, propagate this back to backend by writing text file.
for (DocumentChange dc : queryDocumentSnapshots.getDocumentChanges()) {
String docId = dc.getDocument().getId();
Map<String, Object> docData = dc.getDocument().getData();
String folderPath = createFolderPath(collectionReference, docId, docData);
switch (dc.getType()) {
case ADDED:
System.out.println("Document Created: " + docId);
writeMapToFile(docData, folderPath, "CREATE");
break;
case MODIFIED:
System.out.println("Document Updated: " + docId);
writeMapToFile(docData, folderPath, "UPDATE");
break;
case REMOVED:
System.out.println("Document Deleted: " + docId);
writeMapToFile(docData, folderPath, "DELETE");
break;
default:
break;
}
}
}
});
}
Activityschmidt-sebastian commented on Feb 12, 2018 @ad1269 Thanks for sending this over. This sounds like something that our backend team should take a look out. You shouldn't need to do anything special to work with larger collections.
Can you let us know what collection sizes you are working with? ad1269 commented on Feb 12, 2018 My collection is quite large. It consists of about 100,000 documents, with each document containing about 40 fields.
I've found a work around for my problem by introducing a "Last_Updated" timestamp field, and only querying documents that have been updated within the last few days. Whenever my app changes a document, I also update the Last_Updated field.
I'm happy to provide more information if that'll help the backend team, but I managed to fix my issue! sduskis commented on Dec 6, 2018 The specific issue you have is resolved. There are a lot of ways you can communicate with the firebase team directly: https://firebase.google.com/support/. You can submit additional feature requests or have a discussion about how best to use FireStore with one of those methods.
I'm closing this for now, until there's a new Firestore feature.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|