Ionic 2 & Firebase data downloading too much


#1

I’m using Ionic 2 and Firebase Auth/Realtime Data, but I’m finding that the amount of data being downloaded from Firebase is excessive, and after crunching the numbers, would be far too expensive for me. For example, I’m storing a list of friends as shown below:

Where the path is equivalent to this: /friends/[userId]/[friendId]
And the properties dn & fg mean displayName & following, respectively (I tried to abbreviate the property names to make the download size smaller).

In my Ionic app, I reference the following path: /friends/[userId] to get the list of my friends. In this case there are only 2 users and each user only has 1 friend. Using the “/friends/[userId]” path would just retrieve the single friend for the logged in user.

I can consistently reproduce subscribing to this path and the realtime database usage chart showing that I downloaded 1.9 KB of data each time. This occurs regardless of whether I use an AngularFire2 list or the firebase refs. While subscribed to that list, I can go into the realtime database web UI and manually change my friend’s display name to “Bob Tester2” and again it charges me another 1.9 KB of download usage.

I’m not a math major, but the data for one friend is significantly less than 100 characters, so I assume it would be downloading less than 100 bytes for each change.

Here is my code:

let uid = 'rSq28CjbWFRSmXoCE7aZZBkZKNR2';
let ref = this.db.database.ref(`/friends/${uid}`);	

ref.on('child_added', data => {
	console.log('got friend: ', data.val());		
});
ref.on('child_changed', data => {
	console.log('friend changed: ', data.val());				
});
ref.on('child_removed', data => {
	console.log('removing friend: ', data.val());	
});

When running the app, my output is:

got friend: Object {dn: “Bob Tester”, fg: true}

And that’s 1.9 KB each time!

This is just a sample of the problem. I have other data in my database that all creates the same problem. Basically, opening my app and loading my 1 friend, and the equivalent of 3 twitter posts charges me 20 KB each time.

Is this the expected behavior for Firebase realtime data? I’m I doing something wrong?


#2

If you take advantage of Typescript and always type your variables, questions like this will become easy to answer. The af methods don’t return an Object of your friends type. They return either a Firebase Promise or a Firebase Observable. These Firebase-specific objects differ slightly from “normal” Promises and Observables, but you can cast them to normal Promises and Observables without issues, most of the time. Your code even tacitly acknowledges this, because the part of data that you care about is what is returned by data’s val() method.

For realtime communication, a lot of tools are needed, for example timestamps. Suppose I send you two messages, but they arrive via very different hop-paths across the internet. How does your inbox know which message I sent first? Which message arrives first is not necessarily helpful.

If you want a smaller message bundle, you should look at alternatives to realtime communication.


#3

Hello Aaron, thanks for posting. Are you suggesting that I should expect a 2 KB download for a change to a 100 byte record? This conflicts with the pricing calculator on https://firebase.google.com/pricing/. The calculator states that 5 GB download should equate to 100 million chat messages. That equals 50 bytes per chat message. If each chat message instead required 2 KB download, that would be 200 GB instead of 5 GB. Even if we cut that in half since their 50 byte chat message is 1/2 of my 100 byte friend message, that’s still 20 times what it should be based on their calculator. So although you state this was “easy to answer”, the math doesn’t add up. I understand that there’s other data included during a sync. So either their calculator is a lier or something else is happening that isn’t supposed to.


#4

What is the size of data?


#5

The total storage size in the database (as shown by the “Storage” value in the UI) is 2.6 KB. And that contains a lot more data than is even being subscribed to by this device.


#6

What is the size of data? The variable you use for data.val(). That will answer your main question. Again, focus on the types of variables.

Regarding the chat messages pricing calculator, I believe that is based on Firebase Cloud Messaging, though I’ve never confirmed that with Support.


#7

Here’s a screenshot of the calculator. It’s definitely for the realtime database:

I don’t know how to determine the “size” of a variable in javascript. When I serialize it to a string, it’s the same JSON as in the screenshot of the original post, which is less than 50 characters.

What has your experience been with download sizes from the realtime database?


#8

Why are you arguing with me instead of reading the documentation? It makes me think there is no point talking with you. You create your instant message by extending the realtime database with cloud functions. This will be my last post on this thread.


#9

I’m not sure why you think I’m arguing with you. I’m simply pointing out the errors in your posts. You state that you think my numbers are for cloud messaging, when they clearly are not (you could have looked at my link to the pricing page to figure that out). Cloud messaging is actually free, and if you knew anything about the realtime database or cloud messaging you’d know that. Your responses don’t answer the questions and state inaccurate information with nothing to back it up. My data has screenshots and measurements from Firebase that back it up. I’m sorry that you are frustrated by my posts. If you don’t know the answer, you shouldn’t respond to posts.


#10

Well, that is definitely not the right way to talk to someone that is trying to help you - no matter if you think he has no idea. Pleas consider being a bit less offended and be happy that volunteers spend their time trying to solve your problems.


#11

Thanks @Sujan12. And I’m sorry if I was curt. But the OP would really benefit from reading the docs on how to send an IM in Firebase. He’s acting as though one page explains everything. FCM is fundamentally tied to the realtime database, and if he reads about FCM data messages he will see how to improve his current code. Cloud functions push some of the weight of the bundle back to the server side, instead of forcing everything to come all the way to the client side, which is what the OP’s code is doing now.


#12

Sujan12, I do appreciate others helping me. However, my offended response is geared toward Aaron’s nasty post (quoted below). He bashes me about not reading documentation, which I have and my posts clearly show that I have - hence the numbers from the firebase site, and says that there’s no point talking to me. That’s very rude. In addition, his answer is “You create your instant message by extending the realtime database with cloud functions.” to my question of “Is this the expected behavior for Firebase realtime data?”. The post has nothing to do with chat messages. I’m not trying to send chat messages. I’m just reading a friends list from the database…that’s all. It was wrong of me to post something rude in response to his rude comments, but I’m surprised that you quoted me and not him.