anon
anon

Reputation:

How to create time-expiring data with Firebase Rules?

This talk mentions time-expiring data using Firebase rules at 22:55

https://www.youtube.com/watch?v=PUBnlbjZFAI

How can one do this ?

I didn't find any information regarding this.

Upvotes: 2

Views: 6288

Answers (3)

Luke Pighetti
Luke Pighetti

Reputation: 4841

I recommend two solutions.

1) Use cloud functions to record a message path and the date it was posted. Then every hour sort that list by date, pick all the expired ones, and create a deep update object to null out every expired message. Nowadays you can use Cron Scheduler to handle the periodic flush.

2) Make a rule that says anyone can delete expired messages and make it so that clients automatically delete expired messages when they are in a chat room.

Upvotes: 3

Spri
Spri

Reputation: 97

Same question here.

You can't do it using firebase rules. You should either have a NodeJS backend removing your old data or clients doing it for you. For example, before a client retrieves data, he could remove old data.

Upvotes: 0

Malachi Partlow
Malachi Partlow

Reputation: 39

Written here: https://firebase.google.com/docs/database/security/securing-data

You can't have it auto delete your data but you can make them unreadable (which is the same thing from the user standpoint). Just send a timestamp child field with you data and check against it.

 {
  "rules": {
    "messages": {
      "$message": {
        // only messages from the last ten minutes can be read
        ".read": "data.child('timestamp').val() > (now - 600000)",

        // new messages must have a string content and a number timestamp
        ".validate": "newData.hasChildren(['content', 'timestamp']) && newData.child('content').isString() && newData.child('timestamp').isNumber()"
      }
    }
  }
}

Upvotes: 2

Related Questions