0

I have two collections containing messages from two users. The output of these to the DOM is fine, it displays the messages from user 1 first then user 2 or the other way. This is not good for the chat experience. I need to cycle through the documents first and order them chronologically.

function fetchMessage(){
    dbRef= db.collection("user").doc("eF1RFw3FVAO3LFJ8ORrf7oJPyR13").collection('user1').orderBy("dbTime", "asc");
    dbRef.get().then((snapshot) =>{
        snapshot.docs.forEach(doc =>{ 
            rendermeMessage(doc); //calls these method that outputs and 
                                 //renders to the DOM
        });
    });
}

This other one fetches messages from the second user collection in real-time

dbRef= db.collection("user").doc("eF1RFw3FVAO3LFJ8ORrf7oJPyR13").collection('user2').orderBy("sentAt", "asc");
    dbRef.onSnapshot(function(snapshot) {
        snapshot.docChanges().forEach(function(change) {
            if(change.type==="added"){
                renderAdminMessages(change.doc);
            }
        }); 
    });

I just need it to behave like a normal chat app,i.e the earliest message first, then the second message from the second user if it is the one that comes second in time, like that. I had created an array to first store all the documents from both users there but I cannot cycle through the array. Each document from each collection has a timestamp and a message by the way.

Taio
  • 3,152
  • 11
  • 35
  • 59
  • Don't model messages from each individual user into a collection for that user. Instead model chat rooms. If you have a 1:1 conversation between two users, that would be a chat room, where you have the messages for those two users. That way, you only have to read from one collection, and the multi-collection ordering problem disappears. Also see my answer [here](https://stackoverflow.com/questions/54527049/angular-firebase-listen-to-foreach-changes-with-subscribe/54527082#54527082), and [here](http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase). – Frank van Puffelen Feb 05 '19 at 20:10
  • This worked really well, thank you so much. I don't know if this warrants another question being posted but how do I set up a real-time listener for this. Currently, this fetches and displays messages chronologically and perfectly. How would I set a listener for when a user is in chat and the other user posts a new message to the collection, needing to be displayed directly – Taio Feb 06 '19 at 16:14
  • 1
    `onSnapshot` should already do that. For every change it will fire again, and the `docChanges()` collection contains the changes. If that doesn't work for you I'd post another question about it, as I'm likely going to close this one against one of the links. – Frank van Puffelen Feb 06 '19 at 16:18
  • kindly refer to the new question https://stackoverflow.com/questions/54558827/set-a-real-time-listener-for-collection-with-documents-with-two-unique-ids-fires – Taio Feb 06 '19 at 17:00
  • query this:orderby('timestamp') it will make indexes and show you the data accourding to time stamp. – Muhammad Abdullah Feb 07 '19 at 16:44

0 Answers0