0

So I have designed a basic Publisher-Subscriber model using rhea in JS that takes an API request for saving data in DB and then publishes it to a queue.

From there a subscriber(code added below) picks it up and tries to save it in a DB. Now my issue is that this DB instance goes through a lot of changes during development period and can result in errors during insert operations.

So now when the subscriber tries to push to this DB and it results in an error, the data is lost since it was dequeued. I'm a total novice in JS so is there a way to make sure that a message isn't dequeued unless we are sure that it is saved properly without having to publish it again on error?

The code for my subscriber:

const Receiver = require("rhea");
const config = {
    PORT: 5672,
    host: "localhost"
};
let receiveClient;
function connectReceiver() {
    const receiverConnection = Receiver.connect(config);
    const receiver = receiverConnection.open_receiver("send_message");
    receiver.on("connection_open", function () {
        console.log("Subscriber connected through AMQP");
    });
    receiver.on("error", function (err) {
        console.log("Error with Subscriber:", err);
    });
    receiver.on("message", function (element) {
        if (element.message.body === 'detach') {
            element.receiver.detach();
        }
        else if (element.message.body === 'close') {
            element.receiver.close();
        }
        else {
             //save in DB
        }
    }
    receiveClient = receiver;
    return receiveClient;
}

1 Answers1

0

You can use code like this to explicitly accept the message or release it back to the sender:

try {
    save_in_db(event.message);
    event.delivery.accept();
} catch {
    event.delivery.release();
}

See the delivery docs for more info.

Justin Ross
  • 126
  • 2