One to one chat - Firebase

Hey there,

I was wondering if anyone ever tried to do a completely functional one to one chat as it’s something we are currently doing. We do have a fully working group chat and that’s fine. Ironically, doing a 1:1 chat is much more difficult in terms of logic as it’s easy to have a UID for group chat, not so much for 1:1 chat.

For 1:1 chat there needs to be UID that’s always unique no matter which of the two persons will open the chat and that is the issue here. I have a user 1 UID(A) and user 2 UID(B) and was thinking of actually creating 1:1 chat UID with a combination of those two, however, there are always two options (A:B, B:A). I have tried to create an object where I tried to order the UID alphabetically, but I might be overthinking this and there might be a much easier way, how to do it.

Anyone has any idea, how to proceed, please?

Hi there, to tell you the truth, i havent made that feature yet, but the way i think about it is having a unique id for the chat, isnt that right?

Absolutely, you definitely need a unique ID for chat, however, the question is HOW to get one. That was actually my other idea, how to make it. Basically, for every 1:1 chat create a new Firebase document with following: User_A_UID, User_B_UID, Chat_UID. But can you imagine looking for a right document when user wants to access a chat? That’s definitely not a best practice and potentially heavy lifting for both BE,FE. (Too many reads on BE and not really cool formula on the FE). It seems rather complicated for something that easy as 1:1 chat is.

Yeah, this way, it will definitely be heavy, but in my opinion, realtime database seems more compatible for something like that, as you can make the chatting realtime, without many reads, (as you only download the new data)

Well, we are using a combination of both. Firestore is for storing users’ data. Chat messages are stored in the Firebase realtime database.

1 Like