I'm having hard time figuring out how to structure multi users chat room FirebaseDatabase
architecture properly.
Basically, the app supports private messages between users (could be between 2 users or multiple users). If it was between 2 users only, the data structure is quite easy really.
I'm trying to reduce the cost (I don't want when the current logged user is having a chat with someone and the observer is getting all the messages that are no related to this particular chat) and structure the data properly. If it was between 2 users only, I could always add the recepient under the current user ID and query only that node. But with multiple users (not even sure how many of them) I need to rely on a chat room Id but I'm having hard time with that.
So, here's the idea, let's say the user selects another to start a chat with. I was thinking about something like this:
The problem that I'm facing with this is maintaining that chatId
. Every time the user is about to send a new message, how do I check if there's already a chat with the same recepient (or recepients) based on the chatId
. This is the confusing part for me. I'm stuck on this for days now and I can't figure out a proper way to maintain that chatId
and query it properly. Any help is welcome.