I am building a small chat app (mongodb) and currently debating the implementation of "new"/"unread" messages. What would be the most efficient way to achieve it?Scenario 1Each message has "unread" property. By default, it's "true". To get the total of "unread" messages I would simply count each message that is unread: true and was not sent by me (aka user sending the request). To make them "unread": false I am going to call markAsSeen endpoint every time new message is received and these conditions are met on the front end:1) message sender != current user2) a chat this message is going to is active. There will be some route on the front end - smth like /chat/:ID, so if :ID == incoming message chat ID (message.chat .id) means the user is currently "reading" that chat and the new message should be immediately marked as unread: falseThat MarkAsSeen endpoint is basically findOneAndUpdate type of thing.Scenario 2Somehow keep a separate collection. When chat is created, each user gets its own document for that particular chat: chat, user, unread_amount: 0. Every time User B send a message to User A, the backend increments unread_amount for the User A. If User A is currently active and present his front end would immediately fire a call to decrement the amount the backend just incremented. Or for sake of simplicity, puts 0. So if User A is not active that number would just keep going up and when he shows up and opens that chat the unread counter would get reset.
Submitted December 04, 2019 at 02:12AM by pink_tshirt
So.. did you implement this?
ReplyDelete