how to detect the device connectivity state while using firebase? [duplicate] - firebase

This question already has answers here:
Check whether there is an Internet connection available on Flutter app
(27 answers)
Closed 2 years ago.
I'm using flutter with firebase, how can I know if my device is connected to the internet or not when I wanna make a query to firestore? if my device is offline what will firestore return ?, I must detect the device connectivity situation, so I can emit a certain state in my app.

The Firestore SDK doesn't provide any way to know if the app is offline. The SDK was designed to work while offline. So, if you perform a query while offline, you will either get cached results, or the query will silently retry in the background.
If you want to test for connectivity, you will need try another method to do that. Or, you could use the Firestore REST API (not the Flutter SDK) and detect if the query failed due to network problems.

Related

How to handle client is offline problem in javscript? [duplicate]

This question already has answers here:
Could not reach Cloud Firestore backend - React native Firebase v9
(2 answers)
Closed 7 months ago.
When the internet connection is good my website works fine but when the internet connection goes down firebase return an error called "Client is Offline".
Also sometimes I get problems like -->
[2022-08-04T10:42:54.098Z] #firebase/firestore: Firestore (9.6.7): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
Please help. Thank you in advance.
We were experiencing this issue with slower (although functioning) internet connections. The only thing that fixed this was adding experimentalForceLongPolling: true to our config. You can read about this here and here. We struggled with this for weeks so hope this helps.

Can a Firebase Realtime Database persist data on web like Cloud Firestore? [duplicate]

This question already has an answer here:
Does Firebase JavaScript API catch-up with server when re-connected
(1 answer)
Closed last year.
I have a web app that uses a Realtime Database to store data. If I go offline while the database is connected, make changes, and reconnect, everything works as intended. However, I'm trying to make my web app work as a PWA. Right now, the database only works offline if the page goes online while being used. If you close the app fully while offline and open the app, it won't load.
I see that Cloud Firestore for web has the enableIndexedDbPersistence() method, which appears to do what I want, although obviously since it's for a different service I can't use it. I also see that Realtime Database for Android has FirebaseDatabase.getInstance().setPersistenceEnabled(true), but since its for Android I can't use it for a web app.
Is there a way to enable this functionality?
Firebase Realtime Database only supports disk persistence in its Android and iOS SDKs. There is no disk persistence in the JavaScript SDK for Firebase Realtime Database.
Also see:
Does Firebase JavaScript API catch-up with server when re-connected

Firebase rules that supports only requests from my apps [duplicate]

This question already has answers here:
Locking down Firebase DB access to specific apps
(2 answers)
Closed 3 months ago.
I have two apps registered in Firebase, say App A and App B. I haven't setup any firebase authentication system but I want to allow requests only from App A and App B. I want to check if the request is coming from my apps and then only perform the actions. If the request is from somewhere else then just reject it.
I want to have this security for Cloud Firestore and Firebase Storage.
Currently, this feature isn't available, some of this will be rolling out in the near future using the new App Check: https://firebase.google.com/docs/app-check
At this time, App check only supports:
Realtime Database
Cloud Storage
Cloud Functions (callable functions)

What prevents clients from taking advantage of your Firebase apiKey in the client code? [duplicate]

This question already has answers here:
Is it safe to expose Firebase apiKey to the public?
(10 answers)
Should I hide firebase api keys into backend? Not due to data security but project cloning issue
(2 answers)
Closed 2 years ago.
I have been messing around with Firebase in a web app and realized there's something I don't understand.
In order for the JS code to send data to the Firebase servers, your apiKey has to be in the JS. But then that means your API key is public, and anyone using your website can inspect the code and just send arbitrary commands to Firebase on your behalf from the dev tools, or from any other site now that they have the API key.
I know I must be missing something, since this would make Firebase not useful. What is it I'm missing? What prevents users from sending arbitrary commands to Firebase with your ApiKey?

Choosing Firestore Collections that need to be synced [duplicate]

This question already has answers here:
With firestore, is it possible to configure offline persistence to set 'off' in a specific document?
(2 answers)
Closed 3 years ago.
I am unable to find details of how Firebase Firestore chooses what to cache locally. The documentation is vague about this. It says that Firestore caches data that the app is actively using.
My requirement is that I need to explicitly set specific collections to be cached locally on the device. Is it possible to do it with Firestore?
My requirement is that I need to explicitly set specific collections to be cached locally on the device.
There is no way you can specify which collection may exist in cache and which not. By default, Firestore caches all data that your application reads. It's all or nothing.
Is it possible to do it with Firestore?
No, it's not.

Resources