I have a created a chat app using firestore(using angularfire wrapper) and angular and it's working. The database structure is like this. I have unique ids for every user. When ever someone(sender) sends a message to other person(receiver) I add the message to sender:
collection("messages") => doc(sender's ID) => collection(receiver's ID) => doc(message ID)
Also, I add the same message to receiver:
collection("messages") => doc(receiver's ID) => collection(sender's ID) => doc(message ID)
Now, When user(sender) opens a chat with another user(receiver) I attach listener(valueChanges) to:
collection("messages") => doc(sender ID) => collection(receiver ID)
each message in the collection is structured like this:
message: 'string message'
profilePic: 'url for profile pic'
senderId: 'unique id of sender'
timestamp: some timestamp
Everything works as expected with this configurations but there are a few problems.
- Whenever a new message is added the the 'valueChanges' listens for the changes and assigns the value to array 'chats'. 'chats' array is used with *ngFor to display the content of messages. Problem occurs when ever I receive changes, the whole list of messages are re-rendered including the profile pics. Ideally, only the most recent changes should be rendered.
- There is no way (or that i can think of) to query for most recent message from every user.