Manage unread firebase chat messages

I create a real-time chat, very similar to skype one. I use firebase as a backend and angularfire on the client side. Basically, all things look clear, but I'm stuck on one thing: showing unread messages.

The application uses a very simple Firebase design: 2 or more users can communicate in the "room" - the root collection with a unique name. A chat message, in addition to text, may contain "metadata" - the sender ID, timestamp, etc.

I just need to emulate this pseudocode:

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

So far, according to this , (can I count the children in the place without getting the actual child data?) And this I am trying to control the number of unread messages in each room by transactions and reset count when viewing. But this is a very difficult part, and I'm curious if we have any recommended approach that can reduce the amount of work?

+4
source share
1 answer

You seem to have answered that very much. There is no WHERE clause in Firebase, and the solution should use snap.numChildren () as frequently asked questions, or use a counter as the second link states.

-- , ( 10kb-), numChildren. , .

, :

  • .

, :

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());
});
+2

Source: https://habr.com/ru/post/1533261/


All Articles