Structure a NoSQL database for a chat application

2019-02-13 23:34发布

Coming from years of using relational databases, i am trying to develop a pretty basic chat/messaging app using FireBase

FireBase uses a NoSQL data structure approach using JSON formatted strings.

I did a lot of research in order to understand how to structure the database with performance in mind. I have tried to "denormalize" the structure and ended up with the following:

{

"chats" : {

    "1" : {
        "10" : {
            "conversationId" : "x123332"
         },
        "17": {
            "conversationId" : "x124442"
        }
    }
},

"conversations" : {

    "x123332" : {

      "message1" : {

        "time" : 12344556,
        "text" : "hello, how are you?",
        "userId" : 10
      },
      "message2" : {

        "time" : 12344560,
        "text" : "Good",
        "userId" : 1
      }
    }
  }
}

The numbers 1, 10, 17 are sample user id's.

My question is, can this be structured in a better way? The goal is to scale up as the app users grow and still get the best performance possible.

1条回答
干净又极端
2楼-- · 2019-02-14 00:01

One case for storing messages could look something like this:

"userMessages": 
    { "simplelogin:1": 
        { "simplelogin:2": 
            { "messageId1": 
                { "uid": "simplelogin:1", 
                  "body": "Hello!", 
                  "timestamp": Firebase.ServerValue.TIMESTAMP },
               "messageId2": { 
                  "uid": "simplelogin:2", 
                  "body": "Hey!", 
                  "timestamp": Firebase.ServerValue.TIMESTAMP } 
                 } 
             } 
         } 

Here is a fireslack example this structure came from. This tutorial builds an app like slack using firebase: https://thinkster.io/angularfire-slack-tutorial

If you want something more specific, more information would be helpful.

查看更多
登录 后发表回答