4

I want to use Firebase to get the current time on server without writing to the database and then reading it.

Any one knows a solution to directly get a current time stamp from Firebase?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
data
  • 739
  • 6
  • 17

2 Answers2

6

Yes there is! It doesn't matter if it is for IOS or for Android, you can write a frunction in Cloud Functions for Firebase which will be as easy as:

exports.currentTime = functions.https.onRequest((req, res) => {
    res.send({"timestamp":new Date().getTime()})
})

You can host this in Cloud Function and get the server timestamp without user interaction.

For more informations on how to set/read a timestamp in a Firebase Real-time database, please see my answer from this post.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for your answer, but how to get this value from the client (lets say for android)? – data Jul 26 '18 at 17:17
  • In the exact same way I have explained in this **[post](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data)**, right? Use the `getTimeDate()` method and that's it. – Alex Mamo Jul 26 '18 at 17:20
  • I mean when I deploy my function, how would this function give me the time? – data Jul 26 '18 at 17:23
  • How is this function triggered and how would it return the current time to my app? – data Jul 26 '18 at 17:25
  • You need to host in Coud Function and then trigger like any other function. Use [cron-job.org](https://cron-job.org/en/) for example, right? – Alex Mamo Jul 26 '18 at 17:34
  • cron job will trigger on a time base, not on demand or request......Sorry if my question is not clear..... all I want is a simple way to get current time from firebase instead of getting current time from android device.....But it seems that with firebase I need to write the timestamp to database and then it will be calculated on the server so I can retrieve it, and I don't want a 2 way method to get the time? – data Jul 26 '18 at 17:42
  • In this case use my answer. You can trigger a function on demand, that's not a problem at all. – Alex Mamo Jul 26 '18 at 17:44
  • But then it would be a 2 way method? not very clean. – data Jul 26 '18 at 17:46
  • THe flow is this: Create a function in Cloud Function -> Trigger it whenever needed -> Use the timestamp. It will be a single way method. You are only reading, not writing. – Alex Mamo Jul 26 '18 at 17:49
  • how to trigger it from my app? I call the url from my app of the function? – data Jul 26 '18 at 17:51
  • Yes, you can. [This](https://firebase.google.com/docs/functions/callable) si how you call functions from your app. It's the official documentations. – Alex Mamo Jul 26 '18 at 17:56
  • Do you think that my answer helped you? – Alex Mamo Jul 26 '18 at 17:56
1

You have to write a firebase cloud function that calculates the time. Deploy that function on firebase. Whenever you request that function it will gives you the current time.

Raj
  • 2,997
  • 2
  • 12
  • 30