How to store users and groups for a chat using Firebase -


i make chat using firebase. need display each users list of group belong , each group members.

because of how firebase designed, , in order have acceptable performance, think making list of groups containing list of members, , each users, entry group belong too.

my first question is, right approach?

if so, second question how can atomically add (or removed) user, i.e. making sure both user added in group , group added user or not added @ (i.e. never stored @ 1 location , make db inconsistent) ?

the data model have proposed consistent recommendations firebase. more detailed explanation outlined in best way structure data in firebase

users/userid/groups groups/groupid/users 

firebase provides .transaction atomic operations. may chain multiple transactions ensure data consistency. may use oncomplete callback function. detailed explanation refer firebase transaction. using same approach keep multiple message counts date dashboard display.

sample code nested transaction:

ref.child('users').child(userid).child(groupid).transaction(function (currentdata) {   if (currentdata === null) {     return groupid;   } else {     console.log('groupid exists.');     return; // abort transaction.   }   }, function (error, committed, snapshot) {     if (committed) {       console.log('start new transaction');       ref.child('groups').child(groupid).child(userid).tranaction(function (currentdata) {         if (currentdata === null) {             return userid;         } else {             console.log('userid exists.');             //add code here roll groupid added users             return; // abort transaction.         }       }, function (error, committed, snapshot) {          if (error) {              //rollback groupid added users in previous transaction          }       })   } }); 

Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

Bubble Sort Manually a Linked List in Java -

asp.net mvc - SSO between MVCForum and Umbraco7 -