To count the unread message, I created a count field and it works like this :
- set count_unread = 0
- If new_message : count_unread++
- If user join chat room : set count_unread = 0
- If user already in chat room, I have a listener to listen database change, then show the message in screen and set count_unread = 0
But, as you know, Firebase charge on every read/write command. So, if this user already in the chat room, every new message will create 2 writes ( set count_unread +1 and set count_unread=0 ). Is there any way to optimize it ?