title: Retrieving data from firebase for chat system
body: <p>Here is my firebase data - 
  <a href="https://i.stack.imgur.com/D4DaI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/D4DaI.png" alt="enter image description here"></a></p>
  
  <p>I store a copy of every message under both the users -</p>
  
  <p>I have 2 questions. -</p>
  
  <p>Q1 -  The query for extracting chat messages between user1 and user2 in an ordered fashion so it can be displayed in the personal chat window - </p>
  
  <p>I wrote the following- </p>
  
  <pre><code>//refuser1 is firebase reference to "https://myapp.firebaseio.com/messages/user1"
  
  var messages = [];        
  refuser1.orderByChild("from").equalTo("user1").once('value', function(s){
  s.forEach(function(childsnap){
  messages.push(s.val());
  });
  });
  
  
  refuser1.orderByChild("from").equalTo("user2").once('value', function(s){
   s.forEach(function(childsnap){
   messages.push(s.val());
   });
  });
  // Now messages array has all messages between user1 and user2 -
  // add code to sort messages in array based on timestamp  /
  </code></pre>
  
  <p>Is this the correct way to store and retrieve data for personal one to one chat?</p>
  
  <p>Q2 - When retrieving data , 'value' and 'child_added' events seem to act differently - See the image below - 
  <a href="https://i.stack.imgur.com/Re1mX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Re1mX.png" alt="enter image description here"></a>
  When i use <code>'value'</code> i get all the child objects but when I use <code>'child_added'</code> I get only the first child in users1 .
  The documentaion says that child_added is triggered for every initial child (assuming this means all the child that were existing before new child is added)
  Is my understanding correct? I was expecting the same returned result for 'value' and 'child_added'</p>