如何使用Firebase存储聊天的用户和群组 [英] How to store users and groups for a chat using Firebase

查看:135
本文介绍了如何使用Firebase存储聊天的用户和群组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Firebase进行聊天。我需要为每个用户显示它们所属的组的列表,并且还为每个组显示所有成员。由于Firebase是如何设计的,为了获得可以接受的性能,我认为列出所有包含成员列表的组,并为每个用户,他们所属的所有组的入口也是。

我的第一个问题是,这是正确的方法吗?如果是这样,我的第二个问题是我怎么可以原子地添加(或删除)一个用户,即确保用户都添加到组中,并添加到组用户还是没有添加(即从来没有存储在1位置,使数据库不一致)?解决方案

数据模型您提出的建议与Firebase的建议是一致的。更详细的解释概述在最好的方式firebase中的结构数据

  users / userid / groups 
groups / groupid / users

Firebase为原子操作提供.transaction。您可以链接多个事务以确保数据的一致性。您也可以使用onComplete回调函数。有关详细说明,请参阅 Firebase交易。我使用了相同的方法来保持仪表板显示的多个消息计数。



嵌套事务的示例代码:

  ref.child('users')。child(userid).child(groupid).transaction(function(currentData){
if(currentData === null){
return groupid;
} else {
console.log('groupid already exists。');
return; // Abort the transaction。
}
},函数(error,commit,snapshot){
if(committed){
console.log('start a new transaction');
ref.child('groups ').child(groupid).child(userid).tranaction(function(currentData){$ b $ if(currentData === null){
return userid;
} else {
console.log('userid已经存在');
//在这里添加代码以回滚添加到用户的groupid
return; //中止事务
}
},功能(呃ror,commit,snapshot){
if(error){
//回滚在前一个事务中添加给用户的groupid
}
})
}
});


I would like to make a chat using Firebase. I need to display for each users the list of group they belong to and also for each group all the members.

Because of how Firebase is designed, and in order to have acceptable performance, I am think making a list of all the groups containing the list of members, and for each users, an entry with all the group they belong too.

My first question is, is it the right approach?

If so, my second question is how can I atomically add (or removed) a user, i.e. making sure both the user is added in the group and the group added into the user or not added at all (i.e. never stored at 1 location only and make the DB inconsistent) ?

解决方案

The data model you have proposed is consistent with recommendations for firebase. A more detailed explanation is outlined in best way to structure data in firebase

users/userid/groups
groups/groupid/users

Firebase provides .transaction for atomic operations. You may chain multiple transactions to ensure data consistency. You may also use onComplete callback function. For detailed explanation refer to firebase transaction. I am using this same approach to keep multiple message counts up to date for a dashboard display.

Sample code for nested transaction:

ref.child('users').child(userid).child(groupid).transaction(function (currentData) {
  if (currentData === null) {
    return groupid;
  } else {
    console.log('groupid already exists.');
    return; // Abort the transaction.
  }
  }, function (error, committed, snapshot) {
    if (committed) {
      console.log('start a new transaction');
      ref.child('groups').child(groupid).child(userid).tranaction(function (currentData) {
        if (currentData === null) {
            return userid;
        } else {
            console.log('Userid already exists.');
            //add code here to roll back the groupid added to users
            return; // Abort the transaction.
        }
      }, function (error, committed, snapshot) {
         if (error) {
             //rollback the groupid that was added to users in previous transaction
         }
      })
  }
});

这篇关于如何使用Firebase存储聊天的用户和群组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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