0

I'm new in Firebase with Android. I'm trying to build nearby chat application, which search for nearby people and make geo query with Geofire library and get the nearby location which has the key equal to nearby user key.

I've done this part already in one activity and display the nearby users list in list view.

Then I make onClick listener in each element ( user) in nearby list view, the click listener doing this :

  1. take the geoLocation id which is equal to nearby user id .

  2. put this id into intent and start new chat activity which is responsible for chating .


My problem start in chat activity. I don't know how to implement the Unique chat ID that's should be equal for 2 user, so that both users will end up in one chat session.

I try to combine the user ID 1 + user ID2 and concatenate this both ID and generate the unique ID , the problem is if each user start the chat activity. But they end up with chat ID with user1 + user2 always. The result is that each user will have it's own session and they end up chatting with himself.

This is my database design:

{
    "users" : {
        "UserID1" : {
            "name" : "Abood" 
        } ,

        "UserID2" : {
            "name" : "Yousef" 
        } ,
    } , 

    "chats" : {
        "chat_userID1_userID2" : {
            "messages" : {
                "messageID1" : {
                    "singleMessage" : {
                        "name " : "Abood" ,
                        "text" : "hello how are you ?" , 
                        "sendIn" : "2016.11.24" 
                    }, 
                    "messageID2" : {
                        "singleMessage" : {
                        "name" : "Yousef" , 
                        "text" : "Im fine , thank you " ,
                        "sendIn" : "2016.11.25" 

                        }

                    }



                }
            }
        }
    } ,

    "chat_meta_data" : {
        "chat_userID1_userID2" : {
            "lastMessage" : {
                "from" : "Yousef" , 
                "date" : "2016.11.25" 
            }
        }
    }
}

I search in stackoverflow and I see the same problem but it's was in javascript , Best way to manage Chat channels in Firebase

I don't know how to implement it in java.

How I generate the chat id from combining both user id 1 + user id 2 , but its should be equal for both user?

Community
  • 1
  • 1
aboodrak
  • 873
  • 9
  • 15
  • My answer to the question you linked describes a good way to generate a reproducible room key for two users. If you're having problems implementing this in your app, share the [minimal code that reproduces where you are stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Nov 25 '16 at 04:59
  • thank you @FrankvanPuffelen , i figure out how to solve this problem in java by using compareTo function which return int and check which int is greater and assign the chat ID depend it . – aboodrak Nov 25 '16 at 23:42

3 Answers3

1

A good approach here would be to combine user ids after sorting them in alphabetically or numerically (lexicographically). For example user id for user 1 is 100 and user id for user 2 is 88 then in this case the node id would be chat_88_100. So both the users would access same node.

Sushant
  • 1,834
  • 19
  • 32
0
String uid1 = "ovBiCKe4rgbLU5rMAT0UMzsNAn82";
String uid2 = "BQlsP0ZPfZOtXRo8oyMr2mBw7482";

int comp = uid1.compareTo(uid2);

String chatID;
if (comp == -1) {
 chatID = "chatID_" + uid1 + '_' + uid2;
}
else {
chatID = "chatID_" + uid2 + '_' + uid1;
}
print(chatID);

Output:- chatID_BQlsP0ZPfZOtXRo8oyMr2mBw7482_ovBiCKe4rgbLU5rMAT0UMzsNAn82
Bholendra Singh
  • 980
  • 7
  • 14
-1

Compare user ID one and two and put the smaller number first, here is an example:

public String getFirebaseKey(int userTwo) {
    int idCurrentUser = user.getId();
    if (userTwo < idCurrentUser )
        return userTwo + "_" + idCurrentUser ;
    return idCurrentUser  + "_" + userTwo ;
}
clubby789
  • 2,543
  • 4
  • 16
  • 32