java - Firebase rooms with limited number of participants -
in firebase, creating "rooms" such chats easy, documented in various samples.
for data structure of chat, use this:
rooms room1 member_count members user1 user2 messages message1 but create create limit on number of participants per room, 3 users per chat room.
how can this?
in docs, 1 thing looked promising using transactions. can verify way go? or wrong approach?
what solution this?
firebase countref = new firebase("https://mychat.firebaseio-demo.com/rooms/room1"); countref.runtransaction(new transaction.handler() { @override public transaction.result dotransaction(mutabledata currentdata) { int oldmembercount = currentdata.child("member_count").getvalue(integer.class); currentdata.child("member_count").setvalue(oldmembercount + 1); // try update member count return transaction.success(currentdata); } @override public void oncomplete(firebaseerror error, boolean committed, datasnapshot currentdata) { if (error != null || !commited) { // rollback value (how? nothing?) } else { // transaction has been commited (value has been saved?) currentdata.child("members").child(current_user_uuid).setvalue(current_user_name); // add user members list } } }); it great if comment on approach. furthermore, 1 cannot satisfied in situation of course if transaction has failed. user still wants join, no matter there user trying join @ same time. to? put code function , call function again in error case?
edit:
to create new room automatically unique id, 1 use push() on firebase reference.
but if want add members room then, problem described above remains. alternative solution set users' priority in member list when joining. when setting priority current timestamp, 1 limit member list callbacks 3 (members). doesn't seem elegant nor clean.
if have fixed (and relatively small) number of participants per room, it'd best use transactions. however, might best create well-named objects each person in chat room, example:
/rooms /<roomid, generated push()> /users one: null two: null three: null joining room (code in javascript, please convert java appropriate);
var userid = "myuserid"; var ref = new firebase("<my-firebase>.firebaseio.com/rooms/<roomid>/users"); ref.transaction(function(users) { if (!users.one) { // claim slot 1 users.one = userid; return users; } else if (!users.two) { // claim slot 2 users.two = userid; return users; } else if (!users.three) { // claim slot 3 users.three = userid; return users; } // room full, abort transaction. return; }, function(err, committed, snapshot) { if (committed && !err) { // joined room successfully. } else { // not join room because full. } }); firebase automatically call transaction function if fails commit value server. in addition code above, you'll need implement security rules prevent users claiming slot that's taken:
{ "rules": { "rooms": { "$roomid": { "users": { "$slot": { ".write": "!data.exists()" } } } } } } you can upload these rules via forge, graphical debugger firebase , should go!
Comments
Post a Comment