Filter specific value at the time of being added, Firebase DB

0

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!

    
asked by Dєηyη Crawford 04.11.2018 в 14:43
source

1 answer

0

To whom it may interest

The answer is simple, I was able to solve it on my own.

Explanation :

In fact, the inconvenience occurs because when you search for the route you require, whether you do not know the specific node where your added node is stored or vice versa, as in the examples above, when using the event child_added this only listen when a child node is added ( as "child_added" means ), what happens in my reference is that I'm listening only when a message arrives, but in my own way of adding the data, the data children that are actually being added are the data of the message in question, so when using child_added it does not return any new data. On the other hand if we listen with the event value this returns me all data with any change / added / removed that exists, and not only that, but it gives me the general data of the route, which does not happen with child_added since this returns only the data of the specific son added in question.

To avoid confusion :

  

APPOINTMENT : " I tried to sort by child to skip specifying what chat I need, staying as follows ref (" chats "). orderByChild (" messages ") but this send all the chats that contain the path messages / if any is modified ... "

This happens as I said because when I sort by child, I'm just getting all the data that matches the requested child node (in this case "messages") then combining it with value when something happens in my database that affects a child with the path "messages /" the event returns all the data of the general route if any data is changed, but does not mean that using .orderByChild() is not the correct way.

Solution :

To solve the problem we just have to use the event child_changed that does the same thing as child_added but only if any data of the route is changed, returning the specific data of the changed node, which in this case is the body full of the message, which is being changed by adding the child nodes (in this case, the metadata of the message).

Example :

db.ref("chats").orderByChild("messages").on("child_changed", function(snap){
  foo...
});

Short conclusion :

Always keep in mind how data is added, and also how it is required.

Sources :

Detect secondary events - Firebase database

Sorting and filtering data - Firebase database

    
answered by 05.11.2018 / 13:16
source