C# WebSockets and JavaScript sync time

I have events which occur in a C# based application which run on a timer. However, I can’t seem to ever get both server and clients time to sync with one another to show a count down to which the event is going to end.

I am used web sockets to communicate with the client. Here are some of the things which I have done in attempt to achieve a sync but either the client or server is out of sync as timer either finishes before or after rarely ever at the same time.

Sending time in second

I send a simply message like start-timer;100 which triggers 100 seconds. Then I use something like this

function setTimer(type, expire)
            if(cycleTimers[type] == undefined) {
                cycleTimers[type] = {};

            var d = new Date();
            d.setSeconds(d.getSeconds() + expire);

            cycleTimers[type].time = d;
            if(cycleTimers[type].interval != undefined) {

            cycleTimers[type].interval = setInterval(function() {

                var time = Math.abs((new Date() - cycleTimers[type].time) / 1000);

                var minutes = ("00" + Math.floor(time / 60)).slice(-2);
                var seconds = ("00" + Math.floor(time % 60)).slice(-2);

                $(".timer-cycle").html(minutes + ":" + seconds);

                if(time <= 0) {
            }, 500);

The reason for type, is that in the end I would like to run multiple timers and show the time left on the clients screen. However, for purpose of this example it isn’t needed.

Time in seconds with interval

I have send the time in seconds above but instead of converting it to a date I attempted to just use an interval of 1000 and decrease the seconds remaining by one on each cycle. This didn’t work either.

Unix time stamp

I also attempted to convert date time from c# to a timestamp and send it to client for expire time but they were completely different to one another.


this.ExpireTime = DateTime.UtcNow.AddSeconds(Seconds);

I run a task which executes every 500ms to check if this.Expired is less than current date if so then stop cycle. This works fine on emulator as I write to console the time at each tick.

Source: stackoverflow-javascript

React-native and Firebase: Wait for Firebase data

I am currently implementing an application which relies on Firebase data before it can continue. However, I often (unless I deliberately wait) get the result Cannot read property [property] of null. I very much suspect that this is because the firebase request could not be made before I call this object.

Now I am looking for methods to implement a rendez-vous, or a barrier, really anything that implements a checkpoint where all firebase data are received, before it continues. Is there anything in Javascript which helps me to do so, or any library, or any react-native library that helps me to achieve this?

My code looks as follows: (Fb is the firebase interface)

  bookInterestedBike() {
    this.bookedBikeNo = this.interestBikeNo;
    //This is where the firebase checkpoint / barrier should happen, and no progress should be made unless this data was fetched!

  downloadBikeObj() {
    Fb.staticBikes.child(String(this.bookedBikeNo)).once('value', (bikeObj) => {
      this.bikeObj = bikeObj.val();
      console.log("Save object is: ");

  updateBikeDataStartRide() {
    var updateVals = {}
    updateVals[this.interestBikeNo] = {
      bike_no: this.interestBikeNo,
      current_user: "self",
      positionLat: this.usrLat,
      positionLng: this.usrLng
    return false;

  updateUserDataStartRide() {
    var updateVals = {}
    updateVals[this.uuid] = {
      bike_no: this.bookedBikeNo,
      uuid: this.uuid //TODO: remove this in deployment
    return false;

Source: stackoverflow-javascript