Ad

Managing Pushtokens In Firebase Realtime Db

Our app is used by people in the order of >1M and it does not require any kind of login etc. We use firebase sdk with ionic for get push tokens for the devices. Question is how do we store them efficiently. Should we use

const itemsRef = db.list('tokens');
itemsRef.push({ token:"tokenValue" });

The above will keep adding a new node every time a device opens the app. My concern here is avoiding to add same pushToken again. As in the above structure query against a million of nodes to see if push token exists could be a huge performance issue (I think so not sure).

What is the recommendation to deal this? i am feeling query for a pushtoken if it exists could be expensive.

Ad

Answer

In general you can store a lot of keys under a single node. In fact, I don't think there's a documented limit. Although I'm quite sure there's a theoretical value at which it stops working, you're unlikely to hit it in a real app. It's just storing some data, so nothing stop you from storing millions of values here.

The tricky bit is getting this data out of the database again. But for example, if you know the exact location of the data you want to read, and the data size that you're reading is reasonable, the limit it quite high again.

It's only when you want to read/query the list of keys that you run into limitations. The common advice here is that you shouldn't query more than a few hundred thousand child nodes, although in the past few years that number seems to have gone up. But querying (or reading) millions of nodes is going to be a performance concern.

My concern here is avoiding to add same pushToken again.

If you want a value to be unique in the database (no matter if you're using the Realtime Database or Cloud Firestore), you should use that value as the key of a node (or the ID of a document in Firestore). In code that'd be:

const itemsRef = db.list('tokens');
itemsRef.child("tokenvalue").set(true);

Keys are by definition unique within their parent node, so using the push tokens as keys automatically guarantees that you'll only have one child node for each push token.

Ad
source: stackoverflow.com
Ad