2

I am working on a social iOS application which allows posting data to Firebase. However, I would like to clear all posts older than 1 hour. A great example is Instagram Story, where images are removed after 24 hours. The approach I was thinking about included submitting startTime and endTime timestamps and deleting the data client-side, after the endTime has passed the current time. On the other hand, that would require 24/7 users' activity, as users' data would never be deleted if they don't open the application frequently. I believe I would need a server to handle this problem. I would kindly ask you for suggestions and assistance on how to implement this server-side - as I believe it is the only possible way.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    You'll want to look at Cloud Functions for Firebase, which allow you to run snippets of JavaScript code on Google's servers: https://firebase.google.com/docs/functions/use-cases#perform_database_sanitization_and_maintenance. Also have a look at my answer to this question to see how to query for the data to delete: http://stackoverflow.com/questions/32004582/delete-firebase-data-older-than-2-hours – Frank van Puffelen May 15 '17 at 15:08
  • I have created a function in Cloud Functions as in the answer you have sent me. My question is how to execute that function, that is, when the function will be called? I read the documentation and have seen an onWrite event. Do I need to use that event or is there a way to periodically execute the function to remove data older that 1 hour? – Nedim Kurbegović May 15 '17 at 17:18

2 Answers2

6

Solution 1 - Cloud Functions

Login to firebase and then select your app, you will now be on the App Overview page. Select the Cloud Function Item. This will allow you to execute some javaScript based on a trigger, there are many different types of triggers such as database triggers or authentication triggers. In this case, you want the trigger to be based on time.

That is this one:

enter image description here

Firebase explains cloud functions better than I can below:

After you write and deploy a function, Google’s servers begin to manage the function immediately, listening for events and running the function when it is triggered. As the load increases or decreases, Google responds by rapidly scaling the number of virtual server instances needed to run your function.

Lifecycle of a function

The developer writes code for a new function, selecting an event provider (such as Realtime Database), and defining the conditions under which the function should execute. The developer deploys the function, and Firebase connects it to the selected event provider. When the event provider generates an event that matches the function's conditions, the code is invoked. If the function is busy handling many events, Google creates more instances to handle work faster. If the function is idle, instances are cleaned up. When the developer updates the function by deploying updated code, all instances for the old version are cleaned up and replaced by new instances. When a developer deletes the function, all instances are cleaned up, and the connection between the function and the event provider is removed.

source: https://firebase.google.com/docs/functions/

Solution 2 - Server + Cron Jobs (my preference)

  1. Purchase a cheap server on a monthly subscription (don't need anything fancy should be a few dollars a month), will struggle to find a free one that allows Cron jobs though.

  2. write a script that connects to the firebase database and deletes all the posts older than an hour.

  3. on your server dashboard/control panel create a new Cron Job scheduled to run every hour and point it to the script you just wrote.

torinpitchers
  • 1,282
  • 7
  • 13
0

I guess the following example is exactly what you are looking for without having any other server, and you will have to look into the firebase functions docs for setting up: https://github.com/firebase/functions-samples/tree/master/delete-unused-accounts-cron

For this functionality you need to pay.

Hatzen
  • 408
  • 2
  • 9
  • 16