Skip to main content

ServerTime API

ServerTime API​

The tudada server time (epoch ms) to use instead of the device clock when deciding timed rewards (daily/weekly rewards, event periods). Users can change the device clock, so base reward decisions on server time.

Usage has two steps.

  1. Sync once on entry — getServerTime(). After the first success, the SDK re-syncs automatically whenever the app returns from the background.
  2. Read instantly when deciding — getServerTimeSync(). Returns the time projected from the last sync without calling the server. If no sync has ever succeeded it returns null; do not grant in that case.
tip

Calling getServerTime() once more when the reward UI opens keeps you safe in environments that send no resume signal (review, build tests).

getServerTime(options)​

Fetches the server time. On failure, hold the reward.

Options:

ParameterTypeRequiredDescription
successfunction-Success callback
failfunction-Failure callback
completefunction-Completion callback

Success Response:

FieldTypeDescription
nownumberServer time (integer epoch ms, no timezone)
errMsgstringResult message

Failure Response:

FieldTypeDescription
reasonstringFailure reason (table below)
errMsgstringError message
reasonMeaningWhat to do
networkNetwork errorRetry shortly
timeoutResponse timed outRetry shortly
bad_responseMalformed responseRetry shortly
unsupportedThe current environment does not provide server timeIt fails immediately for 5 minutes; after that, the next getServerTime() call (or, once a sync has succeeded, the automatic re-sync on returning from the background) tries again. Hold rewards meanwhile
hiddenThe request was discarded because the app went to the background, or it was called while the page was hiddenCall again after returning to the foreground
TudadaSDK.getServerTime({
success: (res) => console.log('Server time:', res.now),
fail: (res) => console.log('Unavailable:', res.reason),
});

getServerTimeAsync()​

Promise version. Resolves for both success and failure and never rejects. Distinguish failures by the reason field.

const res = await TudadaSDK.getServerTimeAsync();
if ('now' in res) {
console.log('Server time:', res.now);
} else {
console.log('Unavailable:', res.reason);
}

getServerTimeSync()​

Returns the server time projected from the last sync immediately, without calling the server.

Return Value (ServerTimeSync | null):

FieldTypeDescription
nownumberServer time (integer epoch ms). It may lag behind real time by however long the app was suspended, but it never runs ahead
stalebooleantrue while the re-sync after returning from the background has not yet succeeded

Returns null if no sync has ever succeeded.

const t = TudadaSDK.getServerTimeSync();
if (t && t.now >= nextRewardAt) {
grantDailyReward(); // safe even when t.stale — the value never runs ahead
}
note

The "never runs ahead" guarantee excludes clock skew between server instances (NTP drift). This API does not guarantee absolute clock accuracy.