title: Deleting old messages in Firebase based on time and number of entries
body: <p>I have a question that is an extension of <a href="https://stackoverflow.com/questions/15990681/firebase-chat-removing-old-messages">Firebase chat - removing old messages</a></p>
  
  <p>I have built a chatroom that keeps a linear stream of messages in Firebase. In the above question, the answer showed how to delete old messages by using priority (so messages older than 2 days were deleted).</p>
  
  <p>In my chat room, I want to delete messages that are older than two days, but only if there are more than 250 messages. That way, there will always be 250 messages, even if some are older than two days.</p>
  
  <p>I cannot figure out an easy way to do this using priority, even when combined with limits.</p>
  
  <p>I tried doing this by keeping an internal list of all messages, and having the client delete old messages when there were more than 250 of them, but ran into problems because multiple clients could try to delete messages at the same time.  Interestingly, this caused the Firebase code in the browser to become unresponsive, requiring users to Force Quit their browsers (this happened on Firefox, Chrome, Safari, and even Opera).  Needless to say, I'm looking for a better solution.</p>