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.
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
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.