Kelvin
Kelvin

Reputation: 2288

Firebase data structure for chat

I am trying to build an app for employers to chat with employees.

So I have employers, employees and messages.

I did it with $firebaseArray and childs:

recipient > sender > messages

I want to add sender data such as profile images and last message, I'm not how to do it.

- employer_1
    - employee_1
       - SKLDJLKDksdklJS
              - content: "Hello"
              - timestamp: 129081021

Is this the right way to do it or is there a better way? Thanks.

Upvotes: 0

Views: 1497

Answers (1)

ughai
ughai

Reputation: 9880

You might have issues of showing only latest messages or newer messages since any get on employer_1 -> employee_1 will load all messages.

Another alternate might be to have a structure like this:

{
"users":{
  "employer_1":{
     "profile-image":"<url>",
     "last-message":"SKLDJLKDksdklJS",
     ...
     },
   "employee_1"{
       "profile-image":"<url>",
       "user-chat-list":{
          "employer_1":{
              "last-message":"SKLDJLKDksdklJS",
              "message-list":{
                  "SKLDJLKDksdklJS" : 129081021,
                  "ASDCJLKDksdklJS" : 129081021
              }
          }
       }  
   }
 },
 "messages":{
    "SKLDJLKDksdklJS":{
       "content": "Hello",
       "sender":"employer_1",
       "timestamp": 129081021
      }
 }
}

you won't need to fetch all the messages with content for a chat list.

Upvotes: 2

Related Questions