0

I'm creating a simple NodeJS app (as consumer) that receives messages from MQ Broker (Amazon MQ). I was able to receive the messages but when the publisher sends a message to the broker, the consumer does not read the new message (after a couple of minutes of being idle because of no new messages). Eventually, it disconnects and reconnects but at that moment it will only receive new published messages.

enter image description here

Here you can see that message numbers (8,9,12) are not being read. Is this a problem with my consumer app or it has something to do with the publisher or broker?

const mqtt = require('mqtt');
const connect_mqtt = async () => {
  const host = 'SOME_HOST';
  const options = {
    username: 'SOME_USER',
    password: 'SOME_PASSWORD',
    clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8),
    keepalive: 1000
  };

  const client = mqtt.connect(host, options);

  client.on('error', function (error) {
    console.error('Connection Error:', error);
  });

  client.on('disconnect', function (error) {
    console.error('Disconnected', error);
  });

  client.on('close', function (error) {
    console.error('Connection Closed', error);
  });

  client.on('connect', function () {
    console.log('Connecting...');
    client.subscribe('pass-log', function (error) {
      if (!error) {
        console.log('Connected!');
      } else {
        console.error('Subscription Error', error);
      }
    });
  });

  client.on('message', function (topic, message) {
    if (topic === 'pass-log') {
      console.log('Consume', topic, message.toString());
    } else {
      console.log('NOT YOUR TOPIC');
    }
  });
};
connect_mqtt();

As checked from the ActiveMQ, connection is active enter image description here

UPDATE1:

I added keepalive, clean, reconnectPeriod, qos from options matching from the publisher and it seems to work now. I'll do further observation on this.

UPDATE2:

I purposely disconnected the subscriber by exiting my node app while the publisher is sending message to topic. When I re/connect the subscriber, it only reads the last message.

Rye
  • 445
  • 4
  • 15
  • 1
    Why is your client being disconnected? A MQTT client should happily sit connected to the broker even with no messages being published? – hardillb Aug 08 '20 at 10:19
  • @hardillb, I'm also wondering why. I tried turning off my internet and reconnect but didn't received any console errors specified. I play around with keepalive option to see if issue persist. – Rye Aug 08 '20 at 10:24
  • 1
    First off, if you do not want your client to timeout, you need to add the `keepalive: 60000,` line to your `options` variable to enable the PINGREQ function in the node.js MQTT module. Secondly, you are only subscribing to `pass-log` topic. So if your publisher(s) are sending to `pass-log/id/etc` your code will never receive them. Maybe you want to subscribe to `pass-log/#`?? – JD Allen Aug 08 '20 at 16:47
  • Hi @JDAllen, yes I added `keepalive` and still got the same behavior. As of the moment, I am only testing one subscriber so the topic `pass-log` is enough for now. – Rye Aug 09 '20 at 01:34
  • Any update on this guys, as I am also facing this issue in production. I am using `async-mqtt` npm. It is not able to reconnect with broker. Any solution ? – Sunil Sharma Aug 20 '20 at 07:44

0 Answers0