moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

Injecting a timestamp #614

Closed diginfo closed 7 years ago

diginfo commented 7 years ago

My devices do not have Real Time Clocks so they don't know what the time is, they also run on battery power and so they need to connect and disconnect and go back to sleep as fast as they possibly can, so it is not possible for them to connect to a ntp server and find out what time it is, as this would increase connection time and decrease battery life.

I know that it is not a standard feature of MQTT, however I feel it would be a worthy addition given the growth of low power, dumb devices that in many cases also dont know what time it is.

Could this be added as a feature to MOSCA ??

If not, where would be the best place to inject the timestamp in the code ??

Many Thanks

diginfo commented 7 years ago

I have injected it here, not sure if it's the best place ?

If there was an onBeforePublish event it could be done there without hacking the source ?!

Server.prototype.publish = function publish(packet, client, callback) {
    ...

    function stamp(payload){
      try{  
        var data = JSON.parse(payload.toString());
        data._stamp = new Date().getTime();
        return JSON.stringify(data);
      }
      catch(err){
        console.log(err);
        return payload;
      } 
    }

    var newPacket = {
      topic: packet.topic,
      payload: stamp(packet.payload),
      messageId: this.generateUniqueId(),
      qos: packet.qos,
      retain: packet.retain
    };
mcollina commented 7 years ago

At this point, this is not possible in this codebase. You can with Aedes: https://github.com/mcollina/aedes#authorizePublish.