skip to Main Content

I am developing room chat by using flutter. I have already made a structure of firestore database to store chat, here is it :
messages collection :

  • room chat X to Y :
    • dttm 1 (date and time message has sent)

      • content message
      • id sender
      • id receiver
      • dttm
      • isRead
    • dttm 2 (date and time message has sent)

      • content message
      • id sender
      • id receiver
      • dttm
      • isRead
    • etc …

what I make confuse is.. is this the right structure ? because I need to add read and unread status in UI, but lets say user X has 10 new messages from Y, so I need to do looping 10 times to update the isRead status from false to true right ? I am not sure this is the right way to do looping multiple times, is that really good structure of firestore db ? or is there another good way ?

2

Answers


  1. The schema for messages is fine except for the isRead field. A better approach can be to create a ReadStatus collection to store the pending messages for a user for a particular chat.

    Here is a sample schema for ReadStatus collection:

    • userId: reference
    • roomId: reference
    • lastReadAt: timestamp
    • unreadCount: number

    Here instead of looping through the message and updating isRead you can just update the unreadCount.

    Login or Signup to reply.
  2. It all depends on the needs of your specific application, but most apps just track the last message that the user has read for each chat room. So say that you store the ID of timestamp for dttm 1, then you know that everything up to that message has been read, and anything after it has not been read. With this approach you can track the progress of each user in a chat room with a single value.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search