title: Simple communication via MQTT between node apps
body: <p>Hi I'm really new to MQTT and I've read a lot of posts and blogs about it the last days, yet I seem not to fully understand the different parts needed, such as Broker, Clients.</p>
  
  <p>I want two node apps to communicate with each other via a local mqtt service. As far as I understand, this mqtt service is called broker. I managed it to let 2 node apps communicate via a public broker like this:</p>
  
  <p><strong>app1 (sender)</strong></p>
  
  <pre><code> const mqtt = require('mqtt');
  
   // to be changed to own local server/service
   const client = mqtt.connect('http://broker.hivemq.com');
  
   client.on('connect', () =&gt; {
       let id = 0;
       setInterval(() =&gt; {
           client.publish('myTopic', 'my message - ' + ++id);
       }, 3000);
   });
  </code></pre>
  
  <p><strong>app2 (receiver)</strong></p>
  
  <pre><code>const mqtt = require('mqtt');
  
  // to be changed to own local server/service
  const client = mqtt.connect('http://broker.hivemq.com');
  
  client.on('connect', () =&gt; {
      client.subscribe('myTopic');
  });
  
  client.on('message', (topic, message) =&gt; {
      console.log('MSG: %s: %s', topic, message);
  });
  </code></pre>
  
  <p>As this worked, I wanted to move on by replacing the <strong>public broker</strong> with a <strong>private</strong> one. After a while I found <strong><a href="https://www.npmjs.com/package/mqtt-server" rel="nofollow noreferrer">mqtt-server</a></strong> as a node package.</p>
  
  <p>So I tried the following as a third node app, which is supposed to be the broker for <strong>app1</strong> and <strong>app2</strong>:</p>
  
  <p><strong>server (MQTT broker)</strong></p>
  
  <pre><code>fs = require('fs');
  mqttServer = require('mqtt-server');
  let subscriptions = [];
  
  let servers = mqttServer(
    // servers to start
    {
      mqtt: 'tcp://127.0.0.1:1883',
      mqttws: 'ws://127.0.0.1:1884',
    },
    // options
    {
      emitEvents: true
    },
    // client handler
    function (client) {
      client.connack({
        returnCode: 0
  });
  
  client.on('publish', (msg) =&gt; {
    let topic = msg.topic;
    let content = msg.payload.toString();
      // this works, it outputs the topic and the message.
      // problem is: app2 does not receive them.
      // do we have to forward them somehow here?
      console.log(topic, content);
  
      });
  
      client.on('subscribe', (sub) =&gt; {
        let newSubscription = sub.subscriptions[0];
        subscriptions.push(newSubscription);
        console.log('New subscriber to topic:', newSubscription.topic);
      });
    });
  
  servers.listen(function () {
    console.log('MQTT servers now listening.');
  });
  </code></pre>
  
  <h2>Problem</h2>
  
  <p>After adjusting the connection-Uris (both to <code>ws://127.0.0.1:1884</code>) in <strong>app1</strong> and <strong>app2</strong> The server app <strong>receives all messages that are published</strong> and <strong>recognises that someone connected and listens to a specific topic</strong>. </p>
  
  <p><strong>But:</strong> While the server gets all those events/messages, <strong>app2 does not receive those messages anymore</strong>. Deducing that, something with this broker must be wrong, since using the public broker everything works just fine.</p>
  
  <p>Any help is appreciated! Thanks in advance.</p>