4

I'm using Cloud Firestore (NoSQL) to store profile information for users like:

{
  "uid": "abc123",
  "name": "...",
  "friends": [
    "uid": "x234", 
  ]
  ...
}

Now I'm wondering how to structure a direct chat from user to user. I'm thinking of either:

Adding an addional field for each user document like:

"chats": [
 {
    "from": "name",
    "message": "...",
    ...
 },
  ...
]

Or instead of using Firestore for the chat, I consider using Firebase realtime database with a similar structure.

The last approach would have the benefit, that the user-document would not be "bloated" with lots of chat protocols.

I'd need some advice which would structure/implemention would suit this usecase best.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Piwo
  • 1,347
  • 1
  • 12
  • 19
  • 1
    Lets say the user has a list of 'friends' that they can view. You could program the list in a way that when a user clicks on a friend, the code grabs that 'friend's' uid from Firebase. The user could then send a direct message to that uid. The receiver node structure would have a sub-node for messages which would contain the sender uid, message, and date. You could then use a ListView to render the direct messages. I could give you an example if you'd like. – Steve C. Nov 18 '17 at 22:13
  • As far I understand this would be similar to my first approach? An example would be very helpful @SteveC. – Piwo Nov 18 '17 at 22:39
  • 2
    Have you looked at this Firebase example [here](https://github.com/firebase/friendlypix-android)? The same way a user 'likes' or 'comments' on a post, is the exact same way that you would implement direct messaging/chat between specific users. Have a look at that code first and then let me know if you still need help. That is exactly what I used to create an audio chat application (cb radio). ;) – Steve C. Nov 18 '17 at 22:48
  • 2
    It's well explained in the firebase documentation on how to structure yoru database : https://firebase.google.com/docs/database/web/structure-data – Arrabidas92 Nov 19 '17 at 10:34
  • If you are still interested, please see the my edited answer, cheers! – Alex Mamo Sep 29 '18 at 18:30

1 Answers1

11

When you start building an app, first you need to think about the database which is most appropriate for it. If you think about Firestore, you need to know that Cloud Firestore's pricing model for applications that perform very large numbers of small reads and writes per second per client could be significantly more expensive than a similarly performing app in the Realtime Database.

There are also are a few differences between these two databases. If you want to go ahead with Firebase Realtime Database you need to know that you cannot query over multiple properties and it usually involves duplication data or client-side filtering, which in some cases is some kind of messy. Realtime Database does not scale automatically while Firestore, does.

Regarding how to structure a database for a chat application, you need to know that there is no perfect structure for doing that. You need to structure your database in a way that allows you to read/write data very easily and in a very efficient way. Firebase official documentation explains how can you structure a database for a chat app. If you want something more complex, please read this post, Structuring your Firebase Data correctly for a Complex App.

For a better understanding, I recommend you as well to take Firebase free courses, Firebase in a Weekend: Android.

So it's up to you to decide which one is better for you.

P.S: If you are interested, I have also explained in one of my tutorials how you can create a Chat App using Cloud Firestore and Kotlin.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    the reading can be optimized by seperating messages into blocks of 50 messages or on a weekly block and read only if the user scrolls to those messages.. just keep the last 50 messages in the chat ui window and call the rest whenever user needs them.. – DragonFire Mar 14 '20 at 02:04