angularfire - Managing unread messages in firebase chat -
i'm building real time chat, similar skype one. use firebase backend , angularfire on client side. basically, things clear, i've stuck 1 thing - showing of unread messages count.
app uses simple firebase design: 2 or more users can chat in "room" - root collection unique name. chat message except of text can contain "metadata" - sender id, timestamp, etc.
simply, need emulation of pseudo-code:
room.messages.where({ unread: true }).count()
for now, according this, (can count children @ location without retrieving actual child data?) , this i'm trying manage unread messages count per room transactions , reset count when viewed. tricky part, , i'm curious, have recommended approach here, can reduce amount of job?
it seems you've pretty answered it. there no clause in firebase, , solution use snap.numchildren() faq states, or use counter, second link states.
if going fetch chat messages anyway, or it's one-on-one chat total payload hundred kilobytes or less (twenty or 10kb messages), use numchildren. if message payload going rather large, set counters.
so maintain counter of:
- how many messages user has read
- how many messages exist
- the difference number of unread messages
since "messages exist" counter updated multiple users concurrently, you'd use transaction accomplish 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 '+snap.val()); });
Comments
Post a Comment