Terry Lennox
Terry Lennox

Reputation: 30675

How can I deal with message processing failures in Node.js using MQTT?

I'm using a service written in Node.js to receive messages via MQTT (https://www.npmjs.com/package/mqtt) which then writes to a database (SQL Server using mssql).

This will work very nicely when everything is functioning normally, I create the mqtt listener and subscribe to new message events.

However, if the connection to the DB fails (this may happen periodically due to a network outage etc.), writing the message to the database will fail and the message will be dropped on the floor.

I would like to tell the MQTT broker - "I couldn't process the message, keep it in the buffer until I can."

var mqtt = require('mqtt')
var client  = mqtt.connect('mymqttbroker')

client.on('connect', function () {
  client.subscribe('messagequeue')
})

client.on('message', function (topic, message) {
  writeMessageToDB(message).then((result) => {console.log('success'};).catch((err) => {/* What can I do here ?*/});
})

Upvotes: 0

Views: 123

Answers (1)

Richard Dunn
Richard Dunn

Reputation: 6770

Maybe set a timeout on a resend function? Probably should be improved to only try n times before dropping the message, but it's definitely a way to do it. This isn't tested, obviously, but it should hopefully give you some ideas...

var resend = function(message){

    writeMessageToDB(message).then((result) => {

        console.log('Resend success!')

    })
    .catch((err) => {

        setTimeout(function(message){ 
            resend(message);
        }, 60000);

    });
}

client.on('message', function (topic, message) {

    writeMessageToDB(message).then((result) => {

        console.log('success')

    })
    .catch((err) => {

        resend(message);

    });
});

Upvotes: 2

Related Questions