Firestore querying delay - firebase

Check the photo please. Request that goes to firestore.googleapis.com taking so long and it causes delay to other request. Seems like something isn't correct .
How to remove that request? or move it after to the other firestore request?

What you are seeing is the Firestore SDK establishing a socket and keeping it open for realtime updates to any queries you make. All of those queries will reuse that open connection. The SDK always does this on your first query. It is not blocking any other requests from your code.

Related

Extremely high READ hits on website using Firebase Firestore

I have more than 52k hits on my small project which is using NextJS with Firebase 9.
I don't know what the problem is because the data I am using is very limited and I have only two snapshot listener and one Authentication feature. But somehow in just two days Firestore quota has exceeded and there are more than 52k READ hits.
I don't know why that happened because there are no memory leaks and I am also closing the API listeners which come attached with Firebase.
I am providing github link in case anyone wants to take a look https://github.com/jainChetan81/Todo-List
Attached screenshot of Firebase console:
Use of the Firebase console will incur reads. If you leave the console open on a collection or document with busy write activity then the Firebase console will automatically read the changes that update the console's display. Most of the time this is the reason for unexpected high reads. You can go through this answer. Also, currently there are no tools to trace the reads in Firestore. So to limit the Firestore database reads you have to configure security rules.
So, I would suggest you check your Firestore rules. And if not, it is better to contact Firebase Support as this kind of issue needs visibility into your project and they will have access to it.
I am having the same problem as you with my React app. The solution is to use the useEffect hook and call the function that requests for firestore collection;
useEffect(()=> {
getPosts();
}, []);

When does Firestore automatically retry plain document writes?

If I perform an update operation on a document in Firestore, and I know for certain that the document exists, what possible reason, outside of my control, could an error be generated?
And if an error is generated, is the update automatically retried?
Or is this update only automatically retried if there is no connection and Firestore's offline capabilities take over?
If I perform an update-data operation on a document in Firestore, and I know for certain that the document exists, what possible reason, outside of my control, could an error be generated?
A common error that can arise, is when the Firebase servers reject an operation due to improper security rules. Meaning that you are not allowed to do that particular operation.
And if an error is generated, is the update automatically retried?
No, that will not happen while online.
Or is this update only automatically retried if there is no connection and Firestore's offline capabilities take over?
Yes, that will indeed happen. All operations that take place while offline, are added to a queue. Behind the scenes, Firestore SDK tries to reconnect until the devices regain connectivity. Once the user regains the connection, every change that is made while offline will be updated on Firebase servers. In other words, all update operations will be committed on the server, as long as you have proper rules.

How Can I Measure Firestore Response Time?

I'm fetching data that is stored in Firestore to use in my web app. Normally to check the speed of this kind of call, I'd just check the network tab in the chrome dev tools.
But for my Firebase Query, in the network tab it always shows a time of 1 minute (I've learned this is because of the open long polling connection that the JavaScript SDK utilizes).
So is there a way where I can get more specific details on the speed of my Firestore response?

How do I communicate / trigger a Webtask from Firebas?

In an interesting blogpost about 'Firebase Authentication with the Firebase 3.0 SDK and Auth0 Integration', it is stated that:
You can even have Firebase communicate with Webtask!
Now I can imagine the (web)client triggering a Firebase operation and subsequently a Webtask, but not the other way around. Or am I missing something?
Firebase can run as a serverless app, but it can also run on the server. You can even have Firebase communicate with Webtask! (sic!)
I think that paragraph is misleadingly phrased, perhaps it was just added at the last minute to spark interest. You can have a webtask communicate with Firebase, not the other way around. You don't "run Firebase" on your server either.
TL;DR: A client application may call a webtask with an HTTP request, and that task can read/write the database, but not in any other order.
Here's a quick and dirty reality check as of Nov. 2016:
The Realtime Database by itself does not provide you with a way of executing code. This includes responding to database changes and user requests, handling fan-in and fan-out operations, etc. There is no support for webhooks either.
Which means you have to provide your own execution environment for such logic on a custom server, or you can try to cram as much as possible into the client code. This is a pretty exhaustive topic by itself.
Webtasks are short-lived functions that respond to HTTP requests. Their lifecycle always starts with a request, so they are not fit for continuously watching the database for changes. But they are perfectly valid for handling requests coming in from your client application.
As you can store "secrets" for the webtasks, you can authenticate the task on an admin access level. This gives you the possibility to verify client tokens – which should be sent along with the request –; perform complex authorization and validation, and perform RTDB write operations you wouldn't trust the clients with.
Or trigger external services securely. The possibilities are close to endless.

Firebase - uploading images when internet is offline

Firebase has great option of using their database and sending data to their db even if you are offline, and then when the connection is up again, it sends automatically the data to the db.
is it also possible to do it with the Firebase storage like send images even if the internet is off, and then when the internet is on again, it will send the images files automatically?
If so, how can I do it? If not with Firebase, any other option?
Yes. The Firebase Storage client supports resuming uploads.
See the Firebase Storage documentation for uploads (iOS, Web, Android).
From there for Android:
uploadTask = mStorageRef.putFile(localFile);
sessionUri = uploadTask.getUploadSessionUri();
//save the sessionUri to persistent storage in case the process dies.
And then to resume:
//resume the upload task from where it left off when the process died.
//to do this, pass the sessionUri as the last parameter
uploadTask = mStorageRef.putFile(localFile,
new StorageMetadata.Builder().build(), sessionUri);
Update (20160809)
One way to handle the sessionUri:
when you create the uploadTask, get the sessionUri and store it to the app's SharedPreferences.
when the uploadTask completes, remove the sessionUri from the app's SharedPreferences.
when the app restarts, check if there is a sessionUri in the SharedPreferences. If so: resume that upload.
Technically, the accepted answer is incorrect given the OP question, which is,
send images even if the internet is off
The accepted answer talks about resuming a download once started, which correct in detail but does not address the answer correctly.
The corrected answer is "no", you cannot upload an image to Firebase Firestore if the device is not connected to the internet, the upload will fail and there is no auto-restart of the upload operation.
As previously noted, you must be connected to the internet, at lease long enough to start the upload and get an uploadsessionuri URI from Firebase. Once the upload has started then you can resume the upload using this sample code or the code above.
As noted in the documentation, the resume URI is valid for about 7 days but it is the developer's responsibility to ensure that the file contents has not changed since the start of the upload.

Resources