0

My firebase database has a child which is name is Messages and it has children user_id and also it has children which are title and timestamp such as:

  • my firebase database

    • Messages
      • user_uid_1
        • Title
        • timestamp
      • user_uid_2
      • user_uid_3
      • ...

I want to add a addChildEventListener for Messages to determine if there are any change in Messages. this is very easy with this code:

myRef.child("Messages").addChildEventListener(new ChildEventListener()...

how about firebase rules. if I made it public it works. but if I do like that child listener NOT working what should I add to my rules

{
  "rules": {    
   "Messages":{
       "$uid":{
            ".read": "data.child('timestamp').val() > (now - 18000000)",
            ".write": "auth != null"
   }
  },
   "$other":{
     ".read": true,
     ".write": "auth != null"
   }  
}
}

I want to read only messages from the last 30 minutes can be read so that I've added ".read": "data.child('timestamp').val() > (now - 18000000)",.

I think I should add something between "Messages" and "$uid" to addchild listener correctly. thnks!

mehmet
  • 1,558
  • 5
  • 30
  • 41

1 Answers1

2

Firebase security rules are enforced when you attach the listener. If you're allowing reads on /Messages/$uid, then a listener on /Messages will be rejected since you have no read permission on that level.

This is known as rules are not filters in the Firebase documentation, and means that you cannot use security rules to filter data. It has been covered quite a bit here on Stack Overflow already, so I suggest you check out some of the previous questions. The oldest answer I can find is this one.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • So Puffen you said I should add 2 Message nodes, first one for Childlistener second one for if there is a change in message1, it will get data from Message2 0depeds on timestamp. { "rules": { "Message1":{ "$uid":{ ".read": "data.child('tarih').val() > (now - 1800000)", ".write": "auth != null" } }, "Message2":{ ".read": true, }, "$other":{ ".read": true, ".write": "auth != null" } } } – mehmet Nov 16 '17 at 16:53