Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managing unread messages in firebase chat

I'm building real time chat, very similar with skype one. I use firebase as backend and angularfire on client side. Basically, all things are look clear, but I've stuck with one thing - showing of unread messages count.

App uses very simple Firebase design: 2 or more users can chat in a "room" - root collection with unique name. Chat message except of text can contain "metadata" - sender id, timestamp, etc.

Simply, I need an emulation of this pseudo-code:

room.messages.where({ unread: true }).count()

For now, according to this, (Can I count the children at a location without retrieving the actual child data?) and this I'm trying to manage unread messages count per room by transactions and reset count when viewed. But it is very tricky part, and I'm curious, do we have any recommended approach here, which can reduce amount of job?

like image 640
user2620800 Avatar asked Mar 25 '14 01:03

user2620800


People also ask

How can I count unread messages in firebase chat messages in android?

This is a data that is subjective to each thread and each user. A same thread may have two or more users, each having different unseen message count. So, you can use unseenMsgCountData where you can check across a threadId and userId.

How do you count unread messages?

Unread message counts is a feature that informs users the number of messages they haven't read in each channel. The unread message count appears in the channel list region of the ChannelListFragment class. The number of unread messages per channel is displayed below the timestamp of the last received message.

Can firebase be used for chat?

Enable AuthenticationThis app will use Firebase Realtime Database to store all chat messages. Before we add data, though, we should make sure that the app is secure and that only authenticated users can post messages. In this step, we will enable Firebase Authentication and configure Realtime Database Security Rules.


1 Answers

It seems like you've pretty much answered it. There is no WHERE clause in Firebase, and the solution is to use snap.numChildren() as the FAQ states, or to use a counter, as the second link states.

If you are going to fetch the chat messages anyway, or it's a one-on-one chat where the total payload would be a hundred kilobytes or less (twenty or so 10kb messages), then just use numChildren. If the message payload is going to be rather large, then set up the counters.

So you would maintain a counter of:

  • how many messages your user has read
  • how many messages exist
  • the difference is the number of unread messages

Since your "messages exist" counter would be updated by multiple users concurrently, you'd use a transaction to accomplish this without conflicts:

new Firebase(URL_TO_COUNTER).transaction(function(currValue) {
    return (currValue||0)+1;
}, function(err, success, snap) {
    if( err ) { throw err; }
    console.log('counter updated to '+snap.val());
});
like image 107
Kato Avatar answered Sep 17 '22 16:09

Kato