-1

I'm creating a messaging app with react-native & firebase. Its working quite alright.

Now i want to take it a step further by creating private chats. To my understanding, i need to populate the data into subcollections. e.g : chats>chatroom1>data, chats>chatroom2>data e.t.c

I'm currently working with this code

import firebase from 'firebase';

class Fire {
  constructor (props) {
    this.init()
    this.checkAuth()
  }

  init = () => {
    if (!firebase.apps.length) {

    }
  };

  checkAuth = () => {
    firebase.auth().onAuthStateChanged(user => {
      if (!user) {
        firebase.auth().signInAnonymously();
      }
    })
  } 

  send = messages  => {
    messages.forEach(item => {
      const message = {
        text: item.text,
        timestamp: firebase.database.ServerValue.TIMESTAMP,
        user: item.name 
      }

      this.db.push(message)
    })
  }  

  parse = message => {
    const {user, text, timestamp} = message.val();
    const {key, _id} = message
    const createdAt = new Date(timestamp)

    return {
      _id,
      createdAt,
      text,
      user
    }
  }

  get = callback => {
    this.db.on('child_added', snapshot => callback(this.parse(snapshot)))
  }

  off() {
    this.db.off()
  }

  get db() {
    return firebase.database().ref("messages");
  }

  get uid(){
    return(firebase.auth().currentUser || {}).uid
  }
} 

How can I populate the subcollections from this code?

I believe for calling out of specific subcollections, firebase.database().ref("messages/chatroomid"); will do the trick, right?

what i mean by sub collection is this

Currently my JSON tree looks like:

database
    - messages
         - mvhhsjsfurhcb
               - text: "hi"
                 timestamp: 9942313949
                 user: "David"

firebase.database().ref("messages"); calls out the data under messages 

This is what i want

 database
        - messages
               -chatroom1
                   - mvhhsjsfurhcb
                         - text: "hi"
                           timestamp: 9942313949
                           user: "David"
               -chatroom2
                   - mvhhsjsfurhcb
                         - text: "hey, this i room2"
                           timestamp: 9942313949
                           user: "Sam"

Then for firebase.database().ref("messages/chatroom1"); to call out only messages in chatroom1.

What I intend to achieve with this is to create a private chat for users

To give you more insight, if I was to do this with PHP, I would be doing SELECT * WHERE chatroom = :chatroom;

i believe the answer will be related to this.db.push(message). maybe adding another '.' indicating that there's another branch before pushing the message

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
kunlee
  • 591
  • 1
  • 4
  • 15
  • The Firebase Realtime Database doesn't have the concept of collections. Instead the data is stored in one big tree of nodes, also known as a JSON tree. Can you clarify what you mean by "subcollection" in the context of this? – Frank van Puffelen Apr 29 '21 at 22:23
  • But if you want to move from having a single list of messages, to having multiple rooms with messages, you'll indeed add a layer with the room ID in the JSON hierarchy as you're showing. If you're looking for ideas on how to determine keys for the rooms. have a look at https://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Apr 29 '21 at 22:25
  • @FrankvanPuffelen Thank you, i edited the question as requested in your first comment – kunlee Apr 29 '21 at 23:33
  • 1
    That structure looks good for a multi-chatroom app. For private chat between users, and how to ensure each user can find their chatrooms, see the link I gave earlier as it addresses both. – Frank van Puffelen Apr 30 '21 at 00:37
  • i understand the idea of storing userid...my question now is this, `this.db.push(message)` pushes into `datbase>messages>data`.How do i get it to push into `database>messages>chatroomid>data`. I don't think the link addresses that. I'm used to php for my backend, i apologize if its something i should know. @FrankvanPuffelen – kunlee Apr 30 '21 at 07:14

1 Answers1

1

If you want to push the chat message to a specific chat room, you can do:

  this.db.child("chatroomid1").push(message)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807