在Firebase聊天中管理未读邮件 [英] Managing unread messages in firebase chat

查看:167
本文介绍了在Firebase聊天中管理未读邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立实时聊天,与Skype类似。我在客户端使用firebase作为后端和angularfire。
基本上,所有的东西都看起来很清楚,但是我坚持了一件事 - 显示未读的消息数。

应用程序使用非常简单的Firebase设计:2或者更多的用户可以在房间聊天 - 具有唯一名称的根集合。聊天消息除了文本可以包含元数据 - 发件人ID,时间戳等。

简单地说,我需要模拟这个伪代码:



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

,根据这个,(我可以指望一个位置的孩子没有检索到实际子数据?)和这个我试图通过事务来管理每个房间的未读消息数,并在查看时重置计数。但它是非常棘手的部分,我很好奇,我们有任何推荐的方法,这可以减少工作量?

解决方案

似乎你已经很好的回答了。在Firebase中没有WHERE子句,解决方案是使用snap.numChildren()作为FAQ的状态,或者使用计数器,作为第二个链接状态。



如果您要获取聊天消息,或者它是一个一对一的聊天,总有效载荷将是100千字节或更少(二十左右10kb的消息),那么只需使用numChildren。如果消息的有效负载将会相当大,然后设置计数器。

所以你会保持一个计数器:


  • 您的用户阅读的邮件数量
  • 有多少邮件存在
  • 未读消息


    由于您的消息存在计数器将由多个用户同时更新,您将使用事务来完成此操作没有冲突:
    $ b

      new Firebase(URL_TO_COUNTER).transaction(function(currValue){
    return(currValue || 0) )+1;
    },函数(err,success,snap){
    if(err){throw err;}
    console.log('counter updated to'+ snap.val ));
    });


    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?

    解决方案

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

    这篇关于在Firebase聊天中管理未读邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆