I have a timer which is successfully sending messages every 60 seconds and my app is retrieving this data. All good!
However, when my app first starts I want to be able to get the current time of the server based countdown timer so I can display it within the app itself (ie time until leaderboards reset) . Is this possible or is there a better way?
Thanks
SImon
I figured I could do a client side calcultation based on the returned ‘publisheAt’ response value but the timestamp looks wierd. In my case the latest message response for this was: publishedAt":1504542097468
Not sure how I can calculate anything from that (ie currentTime - publishedAt= timerTimePassed)
Is this a valid plan?
Is that seconds since 1970 or something?
…oh wait…I see its a UNIX timestamp.
So it seems that the ‘publishedAt’ parameter in the returned response is actually the time when the message was received and not the time that the message was actually published at. I did a quick check on this by receiving the timestamp multiple times over a few seconds and it was slightly different each time.
Surely, if my timer is set to broadcast every 60 seconds (which it is) a true ‘publishedAt’ response would only change every 60 seconds?
Hi Simon,
I can advise you the following solution (you did not mention what language do you use for your custom business logic so I chose JS)
- create a separate file with timer config (start date, interval)
'use strict';
module.exports = {
interval: 60,
startDate: 1504598760000
}
- use it for initializing your timer
'use strict';
const config = require('../config/test-timer');
Backendless.ServerCode.addTimer({
name: 'test',
startDate: config.startDate,
frequency: {
schedule: 'custom',
repeat: {'every': config.interval}
},
execute(req){
//add your code here
}
});
- create a hosted service that will calculate next tick time of your timer
'use strict';
const timerConfig = require('../config/test-timer');
class TestTimerTick {
/**
* @route GET /
* @returns <Object>
*/
getNextTick() {
const now = new Date().getTime()
const start = timerConfig.startDate
const interval = timerConfig.interval * 1000 //in ms
const nextTick = start + interval * (1 + Math.floor((now - start)/interval))
return {
start: start,
next: nextTick
}
}
}
Backendless.ServerCode.addService(TestTimerTick);
I hope it will be helpful for you and of course, it is not final code and you can change it to make it more proper for you.
Regards Ilya
Thanks, I’m actually trying to use ‘codeless’ but I cant find any way to get getTime() etc using that.
EDIT: Actually, I figured that using javascript is much easier and allows fro a lot more control. Once I switched, it was easy for me to send a header in my message with the current time that the message was sent and have my client side pick that up.
Thanks for the guidance.