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
The schema for messages is fine except for the
isRead
field. A better approach can be to create aReadStatus
collection to store the pending messages for a user for a particular chat.Here is a sample schema for
ReadStatus
collection:Here instead of looping through the message and updating
isRead
you can just update theunreadCount
.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.