1

To count the unread message, I created a count field and it works like this :

  1. set count_unread = 0
  2. 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 ?

boygiandi
  • 598
  • 10
  • 18
  • See https://stackoverflow.com/questions/52126744/how-to-structure-nosql-messages-to-get-unreads-by-1-query/52129025#52129025, and other from here: https://stackoverflow.com/search?q=%5Bfirebase%5D+unread+message+count for some ideas. But the trade-off you're describing is pretty much the same for all of them. – Frank van Puffelen Apr 05 '20 at 03:21
  • Not sure the logic is clear and without understanding the code, any answers would be a guess. For me, if I'm in a chat room and messages are being added, the count_unread would be incrementing for ME only and if there are multiple users, the number of uread messages would be different for each so they would each have their own counter. Then, as I read the message, MY count_unread would decrement. So I dont see where you would have two writes. – Jay Apr 06 '20 at 18:07

0 Answers0