code: function publish() {
    console.log('Client ' + clientId + ' is publishing to topic ' + topics.publish);
    // client.on('message',()=>{});
    // this is just for testing purpouse
    // maybe we dont need to stop and start publishing
    var timer = setInterval(function () {
      client.publish(topics.publish, 'ping');
      if (stopPub === true) {
        clearInterval(timer);
        stopPub = false;
      }
    }, 1000);
  }
docs: publish Start publishing in an interval to your broker this is more for testing then for real usage.