title: Firebase/NoSQL schema for an instant messaging system
body: <p>I'm using Firebase for an app and the built-in real-time capabilities seem well suited for instant messaging. I'm just having a hard time working out in my head how the database should be set up. Ideally, it's something like this:</p>
  
  <pre><code>messages: {
    &lt;messageId&gt;: {
      from: &lt;userId&gt;,
      to: &lt;userId&gt;,
      text: &lt;String&gt;,
      dateSent: &lt;Date&gt;
      dateRead: &lt;Date&gt;
    }
  }
  </code></pre>
  
  <p>And that's all fine for sending messages, but reading message threads becomes difficult. I need to query the (potentially huge) list of messages for messages that match the current thread's sender and receiver, and then order those by <code>dateSent</code>. If that is possible with Firebase's new querying API, then I have yet to figure out exactly how to do it.</p>