duytq94/flutter-chat-demo

Inconsitency in group chat id in different platforms

ark-tik opened this issue ยท 8 comments

Hi,

I wanted to highlight an issue. The way group id is being generated doesn't guarantees uniqueness when same chat app is opened in two different platforms.
Code reference:

if (currentUserId.hashCode <= peerId.hashCode) {

I've tested it in flutter-android and flutter-web platform and following are the different hascodes it generates:

Android:
myId hashCode: 796431288
peerId hashCode: 871147166
generated groupId: qe7zrSoyJuPH4mf6uxpXwr5jRF33-QwodOOpFVVQAFWabU02IBxXiirV2

Web:
myId hashCode: 246537070
peerId hashCode: 58733319
generated groupId: QwodOOpFVVQAFWabU02IBxXiirV2-qe7zrSoyJuPH4mf6uxpXwr5jRF33

Because of this user is not able to see the chat messages generated from different platform.

Thank you,
Kartik

I don't know why your myId is different between platforms. Are you sure that you log in to the same account between android & web?
I tested with android & iOS devices, it isn't different.
I think it must be the same because actually, the user id app using is google account id, log in to the same account google on a different platform will return the same id.

The generated groupId in both cases has same substrings split by -.
Can you please print hashCodes for following strings just to check whether it matches with android or web generated groupId of my output?
String 1: qe7zrSoyJuPH4mf6uxpXwr5jRF33
String 2: QwodOOpFVVQAFWabU02IBxXiirV2

I'm guessing that it should match with android generated groupId of my output.

Edit: Hash codes can change with dart versions as well, so not really sure if it will match. While the implementation in this repo assumes that it should remain same, which is most likely an incorrect assumption.

Oh, I got what you mean.
Are you meaning that the result of String.hashCode in flutter-web will be different with Android?
The result will be the same on Android & iOS, but I haven't tested this case with flutter-web.
Btw if hash codes can change with dart versions as well, I think we have to find another solution to generate a groupChatId. Actually, the current solution is not perfect to handle this case

One hackish solution I can think of is creating a separate total-users collection-document pair which will store the total number of registered users. Every time a new unique user sing-up then total-users has to increment and will be stored in user's document which can be used at the time of creating groupId.

I found the method compareTo maybe it can help.
The aim, in this case, is we should make it as simple as possible, so we prevent access or write down value to data firestore for perf purpose.
Screen Shot 2021-10-21 at 11 32 36 AM

Agreed, this can be much more cleaner solution.

Fixed at 3d7aadc
Many thanks for your reports ๐Ÿ‘