title: How to subscribe to all topics/messages using NodeJS mqtt, emqx
body: <p>I am writing an MQTT subscriber in NodeJS using the <a href="https://www.npmjs.com/package/mqtt" rel="nofollow noreferrer">mqtt</a> package. The goal of this subscriber is to receive all messages coming to any/all topics but looks like the subscription to <code>#</code> (subscribe to all topics) does not seem to work. But when I replace <code>#</code> with an actual topic name it seems to work fine. Not sure why it is not working.</p>
  <p><strong>PS</strong>: the broker I am using is emqx.</p>
  <p>Here is the code below.</p>
  <pre class="lang-js prettyprint-override"><code>const mqtt = require('mqtt');
  
  const TOPIC = '#';
  const HOST = process.env.HOST || 'localhost';
  const PORT = process.env.PORT || 1883;
  const USERNAME = process.env.USERNAME || 'username';
  const PASSWORD = process.env.PASSWORD || 'password';
  const PROTOCOL = 'mqtt';
  
  const clientOption = {
      port: PORT,
      host: HOST,
      username: USERNAME,
      password: PASSWORD,
      protocol: PROTOCOL
  };
  const client  = mqtt.connect(clientOption);
  console.log(`Connecting to mqtt://${HOST}:${PORT}@${USERNAME} topic:${TOPIC} ...`);
  
  client.on('connect', function () {
    console.log(`Connected!`);
  
    client.subscribe(TOPIC, function(err) {
      if(err) {
        console.error(err);
      } else {
        console.log(`Subscription to ${TOPIC} successful.`);
      }
    });
  
    client.on('message', function (topic, message) {
      // message is Buffer
      console.log(`Incoming message to topic = ${topic} ...`);
      console.log(message.toString());
  
      console.log('Preparing outbound message');
      const outboundMsg = {...message, source: topic}
      console.log('Outbound message below');
      console.log(outboundMsg);
    });
  
  });
  </code></pre>