title: Firebase rooms with limited number of participants
body: <p>In <a href="https://www.firebase.com/" rel="nofollow">Firebase</a>, creating "rooms" such as for chats is easy, just as documented in their various samples.</p>
  
  <p>For the data structure of the chat, I would use something like this:</p>
  
  <pre><code>rooms
      room1
          member_count
          members
              user1
              user2
          messages
              message1
  </code></pre>
  
  <p>But now I would like to create to create a limit on the number of participants per room, say 3 users per chat room.</p>
  
  <p>How can you do this?</p>
  
  <p>In their docs, the one thing that looked most promising was using <a href="https://www.firebase.com/docs/transactions.html" rel="nofollow">transactions</a>. Can you verify that this is a good way to go? Or is this the wrong approach?</p>
  
  <p>What about a solution like this?</p>
  
  <pre><code>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 to update member count
          return Transaction.success(currentData);
      }
  
      @Override
      public void onComplete(FirebaseError error, boolean committed, DataSnapshot currentData) {
          if (error != null || !commited) {
              // rollback value (how? just do nothing?)
          }
          else {
              // transaction has been commited (value has already been saved?)
              currentData.child("members").child(CURRENT_USER_UUID).setValue(CURRENT_USER_NAME); // add user to the members list
          }
      }
  });
  </code></pre>
  
  <p>It would be great if you could comment on this approach. Furthermore, one cannot be satisfied in that situation of course if the transaction has failed. The user still wants to join, no matter that there was another user trying to join at the same time. So what do to? Put this code into a function and call the function again in the error case?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>To create a new room with automatically unique ID, one could certainly use <a href="https://www.firebase.com/docs/managing-lists.html" rel="nofollow">push()</a> on a <code>Firebase</code> reference.</p>
  
  <p>But if you want to add members to that room then, the problem described above remains. An alternative solution could be to set users' <a href="https://www.firebase.com/docs/ordered-data.html" rel="nofollow">priority</a> in the member list when joining. When setting their priority to the current timestamp, one could then <a href="https://www.firebase.com/docs/queries.html" rel="nofollow">limit</a> the member list callbacks to 3 (members). But that doesn't seem to be elegant nor clean.</p>