title: Subscription to MQTT broker and get the data being passed
body: <p>I am generating data with a node.js simulator and passing this data to a http route <code>/simulator/data</code></p>
  
  <p>In the application I am listening broker with MQTT mqtthandler.js file which I share below. </p>
  
  <pre><code>//This is mqtthandler.js file
  
  const mqtt = require("mqtt");
  
  class MqttHandler {
    constructor() {
      this.mqttClient = null;
      this.host = "mqtt://localhost:1883";
      this.username = "YOUR_USER"; // mqtt credentials if these are needed to connect
      this.password = "YOUR_PASSWORD";
    }
  
    connect() {
      // Connect mqtt with credentials (in case of needed, otherwise we can omit 2nd param)
      this.mqttClient = mqtt.connect(this.host, {
        username: this.username,
        password: this.password,
      });
  
      // Mqtt error calback
      this.mqttClient.on("error", (err) =&gt; {
        console.log(err);
        this.mqttClient.end();
      });
  
      // Connection callback
      this.mqttClient.on("connect", () =&gt; {
        console.log(`mqtt client connected`);
      });
  
      // mqtt subscriptions
      this.mqttClient.subscribe("value", { qos: 0 });
  
      // When a message arrives, console.log it
      this.mqttClient.on("message", function (topic, message) {
        console.log(message.toString());
      });
  
      this.mqttClient.on("close", () =&gt; {
        console.log(`mqtt client disconnected`);
      });
    }
  
    // Sends a mqtt message to topic: mytopic
    sendMessage(message) {
      this.mqttClient.publish("value", message);
    }
  }
  
  module.exports = MqttHandler;
  </code></pre>
  
  <p>When simulator sending the data to the <code>/simulator/data</code> route, I am getting the value and sending the broker with value topic. I share the post request code and output of simulator below.</p>
  
  <pre><code>var mqttHandler = require("../mqtthandler");
  
  module.exports = function (app) {
    app.get("/simulator", function (req, res) {
      res.render("iot/simulator");
    });
  
    // route to display all the data that is generated
    app.get("/simulator/data", require("./controllers/data").all);
  
    var mqttClient = new mqttHandler();
    mqttClient.connect();
  
    // route to write data to the database
    app.post(
      "/simulator/data",
      require("./controllers/data").write,
      (req, res) =&gt; {
        mqttClient.sendMessage(req.body.value);
        res.status(200).send("Message sent to mqtt");
      }
    );
  
    // delete the data when the stream is stopped or when the app is closed
    app.get("/simulator/data/delete", require("./controllers/data").delete);
  };
  </code></pre>
  
  <p>When I send get request to <code>/simulator/data</code> I am able to see generated data, however this data is not being sent to broker.</p>
  
  <pre><code>//This is output of simulator
  
      [
          {
              "_id": "5ecfadc13cb66f10e4d9d39b",
              "value": "1.886768240197795",
              "__v": 0,
              "categories": []
          },
          {
              "_id": "5ecfadc23cb66f10e4d9d39c",
              "value": "7.351404601932272",
              "__v": 0,
              "categories": []
          }
      ]
  </code></pre>
  
  <p>PS: Broker is created via node-red</p>
  
  <p>I would like to pass this data to broker and see the result with MQTT subscription. However I can not find where am I making mistake.</p>