title: Is there a way to achieve one to one messaging in react native with firebase real time database?
body: <p>I am trying to achieve one-to-one messaging in react native using the firebase real time database. I am very new to react native/firebase and I am not too sure how I can send a message to a specific user uid. I have seen a few links online which uses firebase firestore for group chats but I would like to perform this using the real time database and only one-to-one messaging. So far I have managed to send a message in my app but I am displaying all messages from all users from the database too when logged in as the current user. I am using react-native-gifted-chat for the UI.</p>
  <pre class="lang-js prettyprint-override"><code>// This is file MessagesScreen.js
  
  class MessagesScreen extends Component {
    state = {
      messages: []
    }  
  
    get user() {
      return {
        _id: FireMessage.uid,
      }
    }
  
    componentDidMount() {
      FireMessage.get(message =&gt; this.setState(previous =&gt; ({
        messages: GiftedChat.append(previous.messages, message)
      })));
    }
  
    componentWillUnmount() {
      FireMessage.off();
    }
  
    render() {
      const chat = &lt;GiftedChat messages={this.state.messages} onSend={FireMessage.send} user={this.user} /&gt;;
  
      if (Platform.OS === &quot;android&quot;) {
        return (
          &lt;KeyboardAvoidingView style={{ flex: 1}} behavior=&quot;padding&quot; keyboardVerticalOffset={10} enabled&gt;
            {chat}
          &lt;/KeyboardAvoidingView&gt;
        );
      }
  
      return &lt;SafeAreaView style={{ flex: 1}}&gt;{chat}&lt;/SafeAreaView&gt;;
    }
  }
  
  export default MessagesScreen;
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>// This is file FireMessage.js
  
  class FireMessage {
  
    constructor() {
      this.init()
      this.checkAuth() 
    }
  
    init = () =&gt; {
      if (!firebase.apps.length &amp;&amp; firebaseConfig.apps.length === 0) {
        firebase.initializeApp({
          apiKey: &quot;xxxxxxxxxxx&quot;,
          authDomain: &quot;xxxxxxxxxxx&quot;,
          databaseURL: &quot;xxxxxxxxxx&quot;,
          projectId: &quot;xxxxxxxx&quot;,
          storageBucket: &quot;xxxxxxxxxx&quot;,
          messagingSenderId: &quot;xxxxxxxx&quot;,
          appId: &quot;xxxxxxxxxxx&quot;,
          measurementId: &quot;xxxxxxxxx&quot;
        });
      }
    };
  
    checkAuth = () =&gt; {
      firebase.auth().onAuthStateChanged(user =&gt; {
        if (!user) {
          firebase.auth().signInAnonymously();
        }
      });
    };
  
    send = messages =&gt; {
      messages.forEach(item =&gt; {
        const message = {
          text: item.text,
          timestamp: firebase.database.ServerValue.TIMESTAMP,
          user: item.user
        };
  
        this.db.push(message)
      });
    };
  
    parse = message =&gt; {
      const { user, text, timestamp } = message.val();
      const { key: _id } = message;
      const createdAt = new Date(timestamp);
  
  
      return {
        _id,
        createdAt,
        text,
        user
      };
    };
   
    get = callback =&gt; {
      this.db.on(&quot;child_added&quot;, snapshot =&gt; 
        callback(this.parse(snapshot)));
    };
  
    off() {
      this.db.off();
    }
  
    get db() {
      return firebase.database().ref(&quot;messages&quot;);
    }
  
    get uid() {
      return (firebase.auth().currentUser || {}).uid;
    }
  }
  
  export default new FireMessage();
  </code></pre>
  <p>Realtime Database structure:</p>
  <p><a href="https://i.stack.imgur.com/YYQQq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YYQQq.png" alt="Database structure of messages" /></a></p>
  <p><a href="https://i.stack.imgur.com/7y4KJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7y4KJ.png" alt="Database structure of users" /></a></p>