0

I am using Firebase and trying to build 1:1 conversations. For rooms, I used a structure like this:

- rooms
   - "user1_user2"  // roomName
      - timestampedId1
        - message: "hello"
        - sender: "user2"
      - timestampedId2
        - message: "hey"
        - sender: "user1"

   - "user2_user3"
      - timestampedId3
        - message: "Mew"
        - sender: "user3"

For room names, I used (which I got from this answer):

let roomName = (from<to ? from+"_"+to : to+"_"+from)

However, now I am trying to retrieve, and I got confused. With Firebase, what is the proper structure of creating private rooms and retrieving them?

Should I store 'from' and 'to' individually inside 'roomName' node? But if so, then how can I compare them and list them as descending timestamp (new to old)? I think there should be a way of doing it with one request. But how can I do it with this 'roomName' approach? Or is there any other better way to achieve it?

let roomRef = Firebase(url: self.url + "/rooms")

// some query?
              .observeEventOfType(.Value, withBlock: { details in

  })

What is the proper way of handling this kind of case? Should I change the structure completely or is there a way to query it properly?

Community
  • 1
  • 1
senty
  • 12,385
  • 28
  • 130
  • 260

1 Answers1

2

I would structure it as follows and have three nodes: roommembers, chat and lastmessages:

roommembers
  user1iduser2id
    user1id: true
    user2id: true

chat
  user1iduser2id
    -KIyPwdDfAA6GxMlwLJB  
      message
      userid
      etc
 (-KIyPwdDfAA6GxMlwLJB  is a childByAutoId() which is also your timestamp)

lastmessages
     user1id
         user1iduser2id
            lastmessage
            lastuser
            etc.
     user2id
         user1iduser2id
            lastmessage
            lastuser
            etc.
Peter de Vries
  • 780
  • 1
  • 6
  • 14