title: How to properly pair two users with Javascript
body: <p>Im trying to do a chat where guys gets paired with girls and girls gets paired with guys.</p>
  
  <p>I'm using Firebase to handle the backend. As soon as the user enters the page, he is assigned to a database named "searching". This is what i got for the pairing:</p>
  
  <pre><code>this.ref = firebase.database().ref("searching").orderByChild("sex").equalTo(this.lookingFor).limitToFirst(1).on("child_added", (snap) =&gt; { 
  this.pairedUserUid = snap.val().uid; //Matches the users
  firebase.database().ref("searching").child(snap.val().uid).remove(); //Remove user from searching database
  }
  </code></pre>
  
  <p>If there is only one person looking it works. The problem is when two users are searching for the same thing. For example: </p>
  
  <p>User X is a guy looking for users that are girls; 
  User Y is also a guy looking for users that are girls;</p>
  
  <p>User Z enters app, is a girl and is looking for users that are guys.</p>
  
  <p>User X and Y both gets paired with Z!
  What i REALLY want is only one of them (X or Y) to get paired with Z while the other keeps waiting.</p>
  
  <p>How can i accomplish this?</p>