Due to data pushing (.push()) on the long run, when there would be thousands of messages, it will cause a lot of lag for all of the users due to querying through each of the nodes individually.
I recommend, instead of creating a Chats node reference with all the messages spreaded, organizing the messages into ChatRooms where each child key inside of it is a combination of the senderUid and receiverUid. It could be made by string concatenation or, summing the ASCII values to BigInteger summing both but it's more complicated.
Example:
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
char[] myCharUid = myUid.toCharArray(); for (char ch : myCharUid)
{ sb1.append((byte) ch);
}
char[] hisCharUid = hisUid.toCharArray(); for (char ch : hisCharUid)
{ sb2.append((byte) ch);
}
String myStringUid = String.valueOf(sb1);
String hisStringUid = String.valueOf(sb2);
BigInteger myBigUid = new BigInteger(myStringUid);
BigInteger hisBigUid = new BigInteger(hisStringUid);
chatRoomId = String.valueOf(myBigUid.add(hisBigUid));
Due to data pushing (.push()) on the long run, when there would be thousands of messages, it will cause a lot of lag for all of the users due to querying through each of the nodes individually.
I recommend, instead of creating a Chats node reference with all the messages spreaded, organizing the messages into ChatRooms where each child key inside of it is a combination of the senderUid and receiverUid. It could be made by string concatenation or, summing the ASCII values to BigInteger summing both but it's more complicated.
Example:
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
char[] myCharUid = myUid.toCharArray(); for (char ch : myCharUid)
{ sb1.append((byte) ch);
}
char[] hisCharUid = hisUid.toCharArray(); for (char ch : hisCharUid)
{ sb2.append((byte) ch);
}
String myStringUid = String.valueOf(sb1);
String hisStringUid = String.valueOf(sb2);
BigInteger myBigUid = new BigInteger(myStringUid);
BigInteger hisBigUid = new BigInteger(hisStringUid);
chatRoomId = String.valueOf(myBigUid.add(hisBigUid));