1

I would like to make a one to one chat. Each user can contact another user.

Json structure would be :

{
  "messages" :
       "user1UID_user2UID" : {
             auto generated ID : {
                      "text" : "hello",
                      "timestamp" : 192564646546,
                      "name" : "user1"
             },
             auto generated ID : {
                      "text" : "hi",
                      "timestamp" : 192564646554,
                      "name" : "user2"
             }
         }
}

When user1 connects to the app, he can see the list of every conversation of which he is a part. Let's say he had initiated a conversation with user 2, and user 3 has a conversation with him too.

So we would have the following children :

user1UID_user2UID

user3UID_user1UID

How can I retrieve all the conversations User1 is involved in to ?

constructor(db: AngularFireDatabase) {
    this.messages= db.list('/messages/' + user1UID + "_" + user2UID); //but I don't know user2UID at this moment
}

Can I make a Regex or do I have to store the conversation key (somewhere) every time it concerns him ?

Or I'm completely wrong and I do not look at the problem the right way?

Community
  • 1
  • 1
isy
  • 531
  • 1
  • 12
  • 27

1 Answers1

2

The key naming schema you use for the chat rooms is a variant of my answer here: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase. It's a variant, since you don't seem to order the UIDs lexicographically, which I recommend.

All my proposed algorithm does is generate a reproducible, unique, idempotent key for a chat room between specific users. And while those are very important properties for a data model, they don't magically solve other use-cases.

As often the case in NoSQL data modeling, you'll have to model the data to fit with the use-cases you want. So if your app requires that you show a list of chat rooms for each user, then you should include in your data model a list of chat rooms for each user:

userChatRooms
  user1UID
    user1UID_user2UID
    user1UID_user3UID
  user2UID
    user1UID_user2UID
    user1UID_user3UID
  user3UID
    user1UID_user3UID

Now getting a list of the chat rooms for a user is as easy as reading /userChatRooms/$uid.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807