0

I am making a chat activity using firebase. I list all of the users contact name in my recyclerview and when a particular contact is clicked, i open a new activity called chat.In which user sends a message.To push the message to firebase i make use of foll code

mMessagesDatabaseReference = mFirebaseDatabase.getReference().child("messages");

Now my child is named "messages", but all users will be able to see everyone's message as i have only one child. My question is how should i name my child in firebase database should that it could be unique.For eg if A messages to B and B messages to A,i want a separate child for them.Similarly for other users. Can somebody please help me?

hagrya
  • 95
  • 1
  • 13
  • Who should be able to see the messages? If you're modeling a 1:1 chat room, I recommend creating a room for those users. A good naming convention for the key can be found here: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase – Frank van Puffelen Oct 05 '17 at 17:29

2 Answers2

0

You could do the basic structure that is recommended by the Firebase RealtimeDB documentation:

{
  // Chats contains only meta info about each conversation
  // stored under the chats's unique ID
  "chats": {
    "one": {
      "title": "Historical Tech Pioneers",
      "lastMessage": "ghopper: Relay malfunction found. Cause: moth.",
      "timestamp": 1459361875666
    },
    "two": { ... },
    "three": { ... }
  },

  // Conversation members are easily accessible
  // and stored by chat conversation ID
  "members": {
    // we'll talk about indices like this below
    "one": {
      "ghopper": true,
      "alovelace": true,
      "eclarke": true
    },
    "two": { ... },
    "three": { ... }
  },

  // Messages are separate from data we may want to iterate quickly
  // but still easily paginated and queried, and organized by chat
  // conversation ID
  "messages": {
    "one": {
      "m1": {
        "name": "eclarke",
        "message": "The relay seems to be malfunctioning.",
        "timestamp": 1459361875337
      },
      "m2": { ... },
      "m3": { ... }
    },
    "two": { ... },
    "three": { ... }
  }
}
Rafael Costa
  • 163
  • 9
0

I would recommend creating a structure like this:

/chat

So, instead of just creating a bunch of messages you begin by creating a database structure for each conversation. Kind of like oldskool chatrooms.

Then you push new messages to each chat under a child called "messages". Besides the actual text, each message should contain a reference to the author.

You can have a look at my toy chat project at https://github.com/henrikenblom/chatapp for inspiration. It's written in Kotlin but it shouldn't be that hard to translate into Java.

henrikenblom
  • 132
  • 8
  • I added a link to a Github project where I'm doing this exact thing. But I should also tell you that I'm using Firebase Functions to setup the chat structure. That code is also in the repository, under "functions". – henrikenblom Oct 05 '17 at 15:49
  • Where in functions? – hagrya Oct 05 '17 at 15:57
  • Right here: https://github.com/henrikenblom/chatapp/blob/master/functions/index.ts. More specifically in "setupNewChat". The actual chat node is created from this Activity: https://github.com/henrikenblom/chatapp/blob/master/android-app/src/main/java/com/enblom/chatapp/NewChatActivity.kt But I suggest that you clone the entire project and open it up in either Android Studio or IntelliJ. – henrikenblom Oct 05 '17 at 16:25