My problem is that I am trying to get an automatic response of a specific data if it is added in Firebase , but the drawback is that it is not a specific route that I can place to simply get the added value with child_added
since I have several nodes within the same parent node, this means that instead of having a static structure like db.ref("chats/unicoChat/messages/")
what I have is something like this db.ref("chats/"+currentChat+"/messages/)
(being currentChat
a variable that I can select through x method in my application) so when using it in my application I could select the chat through the id of the same that I get with a snapshot, with a variable and everything perfect ...
But the problem is that I want to receive a notification when a new message arrives, so I need Firebase to send me a response to the message when the specific route is modified, and get a snapshot of it.
I tried to sort by child to skip the specify that chat I need, being as follows ref("chats").orderByChild("messages")
but this sends me all the chats that contain the path messages/
if any is modified, so it does not help me unless there is a way to filter the node that had the modification and that I do not know, if there is one, it could perfectly be a valid answer!
The structure of my database is as follows:
chats
. LQTYGaRNvdE1PNkyrd3
. . chatId: LQTYGaRNvdE1PNkyrd3
. . messages
. . . -LQTYHrHLMqxZIkybn9G
. . . . Date: Sun Nov 04 2018
. . . . Time: 7:00:00
. . . . Message: Hola como estas?
. . . . peerName: denyn_crawford
. . . XCantidadDeMensajes
. . . .
. . . .
. . peers
. . . 1: denyn_crawford
. . . 2: otra_persona
. XCantidadDeChats
. .
. .
Explanation of the structure
Inside chats I have a node with a unique id for each chat and inside I put the metadata of the chat in question, such as the "peers" that are the users that participate in the conversation, the id of it, and the messages .
Inside each chat I have my path messages/
where the messages are stored and each message has a unique id as well, where I keep the necessary metadata such as the time and date, as well as the message in question.
Conclusion
As you can see, the chats have a unique id so I can not create a fixed variable that listens to a specific route to receive the notification, then to summarize:
What should I do to have Firebase send me a callback or snapshot with the specific information of the last modified node / added / removed ? I have a dynamic route to call it?
Thanks in advance!