Structure a NoSQL database for a chat application (using FireBase)

孤街醉人 提交于 2019-12-19 00:59:11

问题


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:


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.



来源:https://stackoverflow.com/questions/35969715/structure-a-nosql-database-for-a-chat-application-using-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!