Here is my firebase data -
I store a copy of every message under both the users -
I have 2 questions. -
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 -
I wrote the following-
//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 /
Is this the correct way to store and retrieve data for personal one to one chat?
Q2 - When retrieving data , 'value' and 'child_added' events seem to act differently - See the image below -
When i use 'value'
i get all the child objects but when I use 'child_added'
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'