Saad Bashir
Saad Bashir

Reputation: 4519

Flutter + Firebase - Synchronized countdown on multiple devices

I want to show a countdown for an activity and it should show same value on each device. For this purpose I am trying to do the following:

  1. Store activity end time using ServerValue.timestamp.
  2. When a user joins activity, get current server time using ServerValue.timestamp and end time from firebase field.
  3. Add the difference in current server time and end time to current time of the user to get user timezone specific end time.

Firstly I wanted to know if my logic is correct. Secondly, how do I add a duration to the server timestamp, for e.g. 60 seconds. In normal dateTime I think you can add duration using .add but couldn't find a method to do for server time. Thank you

I am using firebase_database package to connect to firebase.

Upvotes: 2

Views: 1635

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599176

Yup, that looks like a good approach to me. I recommend reading the process I outlined here: How to implement a distributed countdown timer in Firebase, which is similar to yours.

As you can see there, I don't perform an addition to ServerValue.timestamp, but instead store the interval for the time as a separate value. This is much simpler than trying to add a value to the existing field, which would require an extra read and write and would make the code much more complex.

Upvotes: 2

Related Questions