How to handle seen/unseen chat messages in React/Firebase chat app? [on hold]


How to handle seen/unseen chat messages in React/Firebase chat app? [on hold]



I’m trying to handle seen and unseen message status in one-to-one chat app using React and Firebase realtime database.



Can I store for every send message in the database, seen: false value and when the receiver mount the component (message), to make again request to the database and change the value of “seen” to “true”?



Are too many database requests can be an issue here?



Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.




1 Answer
1



Is it a good practice to store for every send message in the database, seen: false value and when the receiver mount the component (message), to make again request to the database and change the value of “seen” to “true”?



I don't think you need to do it for every message.



Let's say you have User1 and User2 who have a private conversation.



User1 sends two messages:


Id: 1 Message: "hello"
Id: 2 Message: "how are you"



User2 then looks at the messages - you then send to the server:


User: 2 ChatID: abc LastViewedMessage: 2



User1 comes back to the messages, and receives from the server User2's lastViewedMessage id.



I don't see a reason to store it on a "message by message" basis - unless you specifically want the functionality to mark only some messages as viewed.



You could also store the timestamps of the messages, and a "last-viewed-time" rather than message ids.





The only reason you might want to track individual message status, is if the user acknowledges having seen each individual message. But most chat apps simply keep track of the last message the user has seen by storing the ID/timestamp of that message.
– Frank van Puffelen
Jul 1 at 14:10






if user 1 send 5 messages and user 2 read them all, what should be my logic for changing the status to 'seen' on all the read messages ? Should every message still need to have true/false seen value?
– zorro
Jul 1 at 14:26





Forget the concept of a per-message seen true/false. Whereever the messages for the conversation are being stored, you want to include on a per USER basis, the LAST message they have seen. So the logic will be user2 has seen (last-message-id) from the 5 that are sent.
– nxSolari
Jul 1 at 14:29





Yes, I understand that, but in my UI, all messages have seen/unseen status, my question is how exactly I will change the UI status of all previous messages if I know only the id of the last one ? Should I use state? :)
– zorro
Jul 1 at 14:37





Yes - using state makes sense for this. Assuming you are dealing with only 1-1 conversations, you can just store lastSeenMessage in your state, if you are dealing with group messages you'll want to have an object mapping username:lastSeenMessage. For actually showing the "seen message" icon on the individual messages, you would have to have logic which checks if the message has been seen (is message id < lastSeenMessage) and then apply the seen icon to that message.
– nxSolari
Jul 1 at 14:42

Popular posts from this blog

How to add background colour in existing image using Swift?

Moria Casán

How to make file upload 'Required' in Contact Form 7?