2
votes

This is a group chat app using Firebase. I have few data schema option and I don't know what would be better for performance and less data usage.

Let's suppose there are 10 rooms. this rooms is already made.

Each room's id is [A, B, C, D, E, F, G, H, I, J]

Option 1

  • /users

    • /$uid/joined
      • room1 { roomId : A }
  • /rooms

    • room1 { roomId: A, users : [ ... ] }
  • /messages

    • room1 { roomId: A, messages : [ ... ] }

Option 2

  • /users

    • /$uid/joined
      • room1 { roomId : A, lastChecked, joinedAt... }
  • /rooms

    • room1 { roomId : A, users : [], messages : [] }

Which is better? First time I planned to use Option 1. For decreasing data usage, I want to handle room's brief information and messages separately. But during of developing, I realized the firebase's database is handled by specific keys...

For example, when user access to a room,

  1. Update his/her joined room information at /users/$uid/joined, uid needed.

  2. Update room's joined users at /rooms/$roomKey/users, roomKey needed, and I have to query it using room's Id(A)

  3. After access room, Get messages at /rooms/$key/messages and its key is different with above $roomKey.

Above is option 1's scenario, and If I use option 2, I don't need to get messages again in different path. But the reason of why I ask of this question, Then how about rendering "The list of joined rooms" screen? each room should has a brief information such as title, the number of users, the count of unread messages. If I put messages to /rooms, the messages will queried together. right?

I think the messages is the biggest data size. How can I make this data structure?

2

2 Answers

0
votes

I would take the the 2. option because it's easier to manage the security rules, but the 1. option is not wrong or bad ^^

If you want to keep the size of the data small and don't want to waste anything, you can choose this structure:

- users
  - $uid ....

- rooms
  - $roomid
    - info
      - title
      - description
      - ....
    - messages
      - $messageid
      - ....
    - users

Now you load only the child the is required at the moment. But your security rules are still compact.

0
votes

I think the better option is create a collection Room and Messages due the size limit each document so if you need very large or not know the numbers of messages. So is better or more previsable usage something similar that image attachment.

This way you have no problem with message size limit for documents (1Mib). And metadata for rooms and searchable.

Map firestore