My question is that how can i achieve chat messages using from and to variable (column names) that are stored in firebase realtime database.
Below Image will explain my database architecture.
Now i know that i have to fetch records based on "from" and "to" columns values but i have researched that there is not any possible way to do more than one equalTo or orderBy condition then how can i achieve this. i have also one idea of using new key that will be generate from "from and to's" values
Suppose i want to fetch data for user id 70 (here 70 is logged in user id) and 75 (selected user id) then i'll generate new unique of 70_75 but what if scenario is swapped like (75 is logged in user id and 70 selected user id) then i'll generate new unique of 75_70 but i want both unique to match.
Example :
first unique id is 70_75 second unique id is 75_70
but i want only one unique id that will represent both user's message like 7045757. if you guys have any idea that how can i generate new id that will represent both users then please share it. i don't want load all message from server so that will reduce my memory bandwidth usage and save time. I'm ready accept any solution or advice regrading firebase queries.
I'm currently using firebase realtime database in angular 4 with angularfire2 installed.
Apologies if you are unable to understand. Thanks in advance
It depends a bit on the use-case. In the case of a chat app, there is likely no distinction based on who started the chat. So 70_75
and 75_70
are semantically the same. In that case, store the values in a predictable order. E.g. if you order them lexicographically, you end up with 70_75
in both cases, and can thus query for that combination. I explored this in my answer here: http://stackoverflow.com/questions/33540479/best-way-to-manage-chat-channels-in-firebase
In general for chat, I'd recommend putting messages between the same set of users into a common node, the database equivalent of a chat room.
chat_rooms
70_75
-L04....6mg: ...
-L04....uwuj: ...
That way you can find the node/room for the users, you first determine the key of the room for those users by combining their UIDs. Then you simply read the messages from there, instead of having to query a (much longer) list of messages.