React Web V9 Firebase real-time database caching/offline - javascript

I have a requirement in a react project to use firebase real-time db and sometimes this may be during low or no connectivity.
I know that Firestore has a native offline capability but just wondering how could I make firebase db work offline.
The requirements have quite a heavy R/W workload, so I'm trying to avoid Firestore in this instance.
Should I use something like indexeddb? Has anyone had anything similar? And regarding client side rest data encryption would you recommend something?
Many thanks

There is no built-in support for persisting data between reloads in the Firebase Realtime Database SDK for JavaScript/web. It does handle intermittent loss of connectivity, as long as the user doesn't reload the page.
If you want to persist data between page reloads, you will indeed have to implement that yourself - for example by storing the snapshot values in indexeddb.
Also see:
Does Firebase JavaScript API catch-up with server when re-connected

Related

Using Firestore with offline persistence - Desktop (web)

Offline persistence in Firestore enables the browser to store records that were not uploaded to the server (offline) even after the session was closed (Browser exit)
Please see: https://firebase.google.com/docs/firestore/manage-data/enable-offline
However, firestore does not offer any officially supported way to clear the Chace when a user logs out from his session. Please refer to: https://youtu.be/qGAIimfrBB4?t=257
Recently they released the function clearPersistence, but they clearly state that is not meant for security reasons and recommend to disable Persistence if security is an important factor for you. Please see: https://firebase.google.com/docs/reference/android/com/google/firebase/firestore/FirebaseFirestore#clearPersistence()
Note: clearPersistence() is primarily intended to help write reliable
tests that use Cloud Firestore. It uses an efficient mechanism for
dropping existing data but does not attempt to securely overwrite or
otherwise make cached data unrecoverable. For applications that are
sensitive to the disclosure of cached data in between user sessions,
we strongly recommend not enabling persistence at all.
I want to understand better what's the security hole with using "ClearPersistence" on logout of the user.
Anyone experienced with that? Any other working solution that enables you to remove all the Firestore cache after a logout?
There is no guarantee that your code will run in the browser (or any other client). For example: a malicious user can take the configuration data from your application, and call the API to get access to the same data in your project, but then store it wherever they want.
Another malicious user might prevent the app from clearing the local cache, or quickly copy the local cache file to another location to have a copy before it is cleared.
And these are just two if the simplest examples. The simple fact is that you should assume that any data that exists/persists on the client can be seen by any user who has access to that client.

Saving Firebase RTDB bandwidth by performing initial sync from JSON file

Firebase Realtime Database offers powerful synchronisation across clients... But it does seem to have rather high bandwidth charges.
I was hence wondering if it is possible to perform an "initial sync" on clients, so to speak, where we load a reasonably recent version of the database from elsewhere (with lower bandwidth charges), and then have the Firebase SDK (flutter, web) sync from there instead of downloading all the required parts of the database. Maybe a "load from JSON" sort of function or similar.
Seeing as to how the Firebase SDKs do seem to store some state locally on clients (for offline operation) and sync up with Firebase once online again, I was wondering if it might be possible to:
set the local state to our "recent version" downloaded from another source, along with that version's timestamp
trick the client SDK into thinking that we are recovering from an "offline" state
then let the SDK communicate with the Firebase server to get changes since our last timestamp
Are there any approaches to achieve the effect of saving bandwidth through performing an initial sync from a cheaper source? Thanks in advance!
Unfortunately every get() and every realtime listener will load all data it needs from the database. I had the same idea when started working with the Firebase RTDB but unfortunately at this moment it's not possible.
The only way to reduce the bandwith is to read data in smaller chunkgs as possible.
You might want to take a look at AceBase, which is an open source alternative for the Firebase RTDB. It offers the same functionality, has powerful indexing and querying options, offline support, synchronization etc. Easy to setup and you can host it anywhere you want. You can even use it as a standalone realtime database in the browser, so you could also use it in combination with Firebase to perform custom synchronization between your front and backend db's. AceBase is free and its full source code is public.

How to cache only selected documents in Firebase Firestore in JavaScript SDK

I'm making a chat app and I want Firebase Firestore database to cache only that messages which are sent by me or for me. How can I do this?
Can I control what documents are cached in Firestore database? Thanks.
The Firestore SDK provides no direct control over the cache, other than its total size, and whether or not it's active. The documentation describes everything you can do.
One alternative is to only use the SDK to read documents that should be cached, and use the REST API for all other reads that should not be cached.

Best practice for communication between app and server when using PhoneGap/Cordova

I'm working on a PhoneGap project using Ionic. It's basically a chat app, so I need the user to be able to register, login and send messages using a backend API on my server. Naturally this needs to be secure, so I'm wondering what the best way to securely communicate with an API endpoint is, when using a AngularJS and PhoneGap.
Ideally, it should not require a server cert, as currently I don't have the funds to purchase one. In previous projects, I used a method where each account was assigned an ID, and a hash consisting of a secret + their ID, which had to be included with each request to ensure that the user couldn't forge requests from another ID, however I don't know how secure this method is.
Any tips, suggestions or read material would be really appreciated. I understand this question sounds subjective, so if possible please answer based on facts, security disclosures and any documentation on methods.
I know the solution to all your needs and it is called Firebase.
How your requirements will be met by firebase:
1. You are using Ionic to build your hybrid app(you are cool!) and that means AngularJS.. Firebase has the perfect library called AngularFire, that uses AngularJS to interact with the firebase servers.
You are building a chat app, awesome! Firebase has real time syncing between your app and database. That is a lot of work saved for you by Firebase (Claps).
You need to register users, Firebase has super easy user register management(both OAuth and manual registration)
Security! It is super important and Firebase has you covered even here. Implementing user level security is super simple using some simple json format security rules. I will quote this from the site "The safety and security of your data is our top priority. Firebase requires 2048-bit SSL encryption for all data transfer and allows you to restrict reading and writing via granular access controls and custom authentication.
All data is replicated and backed up to multiple secure locations."
It is free(upto some level. Do some research about it, I am not sure).
Your basic id + hash security measure is not bad at the same time not perfect or dependable. Firebase has you covered here through simple login and read/write rules and as well as some closed sourced security.

Online/offline data updating mobile best practices

I have a requirement to create a mobile app that has the ability to queue up records that need to be saved when no internet connection is available and update the data via web api when a connection is available in our SQL Server DB.
I'm trying to avoid having a local version of the DB on the device which I've seen some others recommend in other questions on stackoverflow.
The approach I'm going with right now is to try and save the records in a local store (JSON) while there is no connection. Then to try and hook in some event to know when the connection has been re-established to update my data to SQL Server.
I'm just wondering what the best practices are around this? Are there issues with my current way of trying to solve this problem?
We are using DevExpress's DevExtreme mobile solution with Visual Studio 2012 and Cordova PhoneGap.
Any advice would be very much appreciated.
How big is the JSON data that you want to store, likely to get?
Depending on your answer, you could store it (the JSON data) in localStorage and then write it next time the device is connected.

Categories