title: Computing firebase message size
body: <p>I am trying to compute the size of messages in firebase in order to accurately estimate the cost of my app.</p>
  
  <p>I noticed when running that the realtime database calculator was showing larger than expected data sizes. To verify this, I started a toy application that has a single ref called "test" with the data:</p>
  
  <pre><code>{"foo": "bar"}
  </code></pre>
  
  <p>Going on other answers, my estimate is that this data is under 20 bytes. </p>
  
  <p>Retrieving the data with this code:</p>
  
  <pre><code>firebase.database().ref("test").once("value", function(snapshot) {
    console.log(snapshot.val());
  });
  </code></pre>
  
  <p>Here is a <a href="https://jsfiddle.net/q3z5jkh2/" rel="noreferrer">jsfiddle showing this toy example</a>.</p>
  
  <p>I grab the ref and console.log the data. I have accessed this example under 10 times. When I look at the toy application's realtime database usage tab, it shows something like 30KB bandwidth used.</p>
  
  <p>What other data is being sent to account for this large gap in expected data usage (10 * 20 bytes = 200 bytes) vs the actual 30KB sent?</p>
  
  <p>Is there some initial overhead when initializing an app that adds to the data usage?</p>
  
  <p>EDIT:</p>
  
  <p>Following cartant's advice, I logged the frames being sent from the websocket. Here is what I found (Before this I see some initialization messages of about 200 bytes):</p>
  
  <pre><code>     Data                                                        Length    
       {"t":"d","d":{"r":22,"a":"q","b":{"p":"/test","h":""}}}     55 
       {"t":"d","d":{"b":{"p":"test","d":{"foo":"bar"}},"a":"d"}}  58 
       {"t":"d","d":{"r":23,"a":"n","b":{"p":"/test"}}}            48 
       {"t":"d","d":{"r":22,"b":{"s":"ok","d":{}}}}                44 
       {"t":"d","d":{"r":23,"b":{"s":"ok","d":""}}}                44
  </code></pre>
  
  <p>So it seems like there is a ~200-250 byte overhead for any message. Can anyone confirm this? This still does not fully explain the gap I mentioned earlier (10 messages * 250 bytes = 2.5 KB vs the 30 KB recorded).</p>
  
  <p>UPDATE:</p>
  
  <p>The current bandwidth usage is up to 155 KB. I am not sure how this number is possible with 35 viewers on this post. In order to try to get a sense of this (I am still not sure how bandwidth is actually calculated), here are my thoughts:</p>
  
  <pre><code>200 bytes to initialize/connect
  220 bytes per message (200 bytes of overhead + 20 bytes in message)
  100 times sent (this is probably an overestimate, as there are 35 views on this post, but I have viewed it around 10 times myself)
  
  (200 bytes + 220 bytes) * 100 views = 42000 bytes or 42 KB. 
  </code></pre>
  
  <p>So to get to 155 KB either this was sent much more than 100 times, or there is some unexplained overhead. Also, I assume (which I do not know) that the overhead to initialize is 200 bytes and the overhead to send any message is 200 bytes.</p>