0

I'm trying to get started with Firebase and I just want to make sure that this data structure is optimized for Firebase.

The conversation object/tree/whatever looks like this:

conversations: {
  "-JRHTHaKuITFIhnj02kE": {
    user_one_id: "054bd9ea-5e05-442b-a03d-4ff8e763030b",
    user_two_id: "0b1b89b7-2580-4d39-ae6e-22ba6773e004",
    user_one_name: "Christina",
    user_two_name: "Conor",
    user_one_typing: false,
    user_two_typing: false,
    last_message_text: "Hey girl, what are you doing?",
    last_message_type: "TEXT",
    last_message_date: 0
  }
}

and the messages object looks like so:

messages: {
  "-JRHTHaKuITFIhnj02kE": {
    conversation: "-JRHTHaKuITFIhnj02kE",
    sender: "054bd9ea-5e05-442b-a03d-4ff8e763030b",
    message: "Hey girl, what are you doing?",
    message_type: "TEXT",
    message_date: 0
  }
}

Is storing the name relative to the user in the conversation object needed, or can I easily look up the name of the user by the users UID on the fly? Other than the name question, is this good? I don't want to get started with a really bad data structure.

Note: Yes, i know the UID for the conversation & message are the same, I got tired of making up variables.

Hobbyist
  • 15,888
  • 9
  • 46
  • 98

2 Answers2

0

I usually model the data that I need to show in a single screen in a single location in the database. That makes it possible to retrieve that data with a single read/listener.

Following that train of thought it makes sense to keep the user name in the conversation node. In fact, I usually keep the username in each message node too. The latter prevents the need for a lookup, although in this case I might be expanding the data model a bit far for the sake of keep the code as simple as possible.

For the naming of the chat: if this is a fairly standard chat app, then user may expect to have a persistent 1:1 chat with each other, so that every time you and I chat, we end up in the same room. A good approach for accomplishing that in the data model, can be found in this answer: Best way to manage Chat channels in Firebase

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • The issue with storing everything under a single location is that when loading a 'conversation' (Actually a different relation in my application, but conversation makes it easier) the messages aren't always relevant. Also the messages between the users should not all be loaded at once, which is required if they're all under the same object, even if let's say we only wanted to load the 50 most recent messages. Storing it all in a single location affects performance with scale. Also, this allows for queries on the 'conversation' without loading the messages. – Hobbyist Jun 18 '16 at 22:52
0

I don't think you structured it right. You should bare in mind "What if" complete analysis.

Though, I would recommend structuring it this way (I made it up for fun, not really tested in-terms of performance when getting a huge traffic. but you can always do denormalization to increase performance when needed):

{
  "conversation-messages" : {
    "--JpntMPN_iPC3pKDUX9Z" : {
      "-Jpnjg_7eom7pMG6LDe1" : {
        "message" : "hey! Who are you?",
        "timestamp" : 1432165992987,
        "type" : "text",
        "userId" : "user:-Jpnjcdp6YXM0auS1BAT"
      },
      "-JpnjibdwWpf1k-zS3SD" : {
        "message" : "Arya Stark. You?",
        "timestamp" : 1432166001453,
        "type" : "text",
        "userId" : "user:-OuJffgdYY0jshTFD"
      },
      "-JpnkqRjkz5oT9sTrKYU" : {
        "message" : "no one. a man has no name.",
        "timestamp" : 1432166295571,
        "type" : "text",
        "userId" : "user:-Jpnjcdp6YXM0auS1BAT"
      }
    }
  },
  "conversations-metadata" : { // to show the conversation list from all users for each user
    "-JpntMPN_iPC3pKDUX9Z" : {
        "id": "-JpntMPN_iPC3pKDUX9Z",
        "date":995043959933,
        "lastMsg": "no one. a man has no name.",
        "messages_id": "-JpntMPN_iPC3pKDUX9Z"
    }
  },
  "users" : {
    "user:-Jpnjcdp6YXM0auS1BAT" : {
      "id" : "user:-Jpnjcdp6YXM0auS1BAT",
      "name" : "many-faced foo",
      "ProfileImg" : "...."
      "conversations":{
            "user:-Yabba_Dabba_Doo" : {
                "conversation_id": "-JpntMPN_iPC3pKDUX9Z",
                "read" : false
            }
      }
    },
    "user:-Yabba_Dabba_Doo" : {
      "id" : "user:-Yabba_Dabba_Doo",
      "name" : "Arya Stark",
      "ProfileImg" : "...."
      "conversations":{
            "user:-Jpnjcdp6YXM0auS1BAT" : {
                "conversation_id": "-JpntMPN_iPC3pKDUX9Z",
                "read" : true
            }
      }
    }
  }
}
ALI MAKEEN
  • 403
  • 3
  • 8