2

I'm making a friends list chrome extension for an online browser game I play. One feature is that friends can be able to chat with one another. The database I'm using is called firebase which stores its data in a JSON tree format.

My database has this structure:

   Users
     |
     |_USER1
     |     |
     |     |__FRIENDS
     |     
     |_USER2
           |
           |__FRIENDS

I'm trying to figure out what would be the best way to store chats as part of this database. The option I'm leaning towards right now would just keep a copy of the two users chats in both their section of the Users directory, looking like this:

Users
     |
     |_USER1
     |     |
     |     |__FRIENDS
     |     |
     |     |__CHATS
     |           |
     |           |__chat w/USER2
     |     
     |_USER2
           |
           |__FRIENDS
           |
           |__CHATS
                 |
                 |__chat w/USER1  

This would make it so on each message send I'd have to update two objects, one in each users section. Note since the tree is formatted as 'key/value' pairs, in the CHAT section of each user the keys would be the other user's name, while the value would be the list of messages sent.

Is this a decent way of organizing such a database? The game is pretty small so I'm not expecting huge traffic.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MarksCode
  • 8,074
  • 15
  • 64
  • 133

1 Answers1

2

When it comes to the Firebase Database (and most NoSQL data stores), it's often best to flatten your data.

Users
     |
     |_USER1
     |     |
     |     |__FRIENDS
     |     
     |_USER2
           |
           |__FRIENDS
UserChats
     |
     |_USER1
     |     |
     |     |__chat w/USER2
     |     
     |_USER2
           |
           |__chat w/USER1  

This way you can look up the user's friend list without having to load their list of chats.

Also look at this answer about a convenient scheme for constructing 1:1 chat room identifiers: Best way to manage Chat channels in Firebase

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I'll take your suggestion and separate the chats into another part of the database. Your provided link provides a way to only have to update 1 object on each message send, so I'll try that. Thanks. – MarksCode Jul 20 '16 at 19:54