Search code examples
node.jstypescriptamqp

Node Js / Typescript - AMQP Consumer


I am trying my hand at node.js/typescript for the first time and having a bit of trouble making a consumer for a rabbit queue.

Code:

let amqp = require('amqp');

let connection = amqp.createConnection({url: "amqp://" + RABBITMQ_USER + ":" + RABBITMQ_PASSWORD + "@" + RABBITMQ_HOST + ":" + RABBITMQ_PORT + RABBITMQ_VHOST});

connection.on('ready', function() {
    connection.exchange(RABBITMQ_WORKER_EXCHANGE, function (exchange) {
        connection.queue(RABBITMQ_QUEUE, function (queue) {
            queue.bind(exchange, function() {
                queue.publish(function (message) {
                    console.log('subscribed to queue');
                    let encoded_payload = unescape(message.data);
                    let payload = JSON.parse(encoded_payload);
                    console.log('Received a message:');
                    console.log(payload);
                })
            })
        })
    })
})

It seems to connect to the amqp server and throws no errors but it just sits there and doesn't consume anything. Is there a step I am missing?

Any help would be greatly appreciated, Thank you.


Solution

  • Here is my solution that is working based off of amqp's JS tutorial. https://www.rabbitmq.com/tutorials/tutorial-three-javascript.html

    Probably not up to TypeScript standards, feel free to correct me if there's a better way.

    #!/usr/bin/env node
    
    require('dotenv').config();
    import amqp = require('amqplib/callback_api');
    import db = require('./database');
    
    amqp.connect({
        protocol: process.env.RABBITMQ_PROTOCOL,
        hostname: process.env.RABBITMQ_HOST,
        port: process.env.RABBITMQ_PORT,
        username: process.env.RABBITMQ_USER,
        password: process.env.RABBITMQ_PASSWORD,
        vhost: process.env.RABBITMQ_VHOST
    }, function(err, conn) {
        conn.createChannel(function (err, ch) {
            // set exchange that is being used
            ch.assertExchange(process.env.RABBITMQ_WORKER_EXCHANGE, 'direct', {durable: true});
            // set queue that is being used
            ch.assertQueue(process.env.RABBITMQ_QUEUE, {durable: true}, function (err, q) {
                console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
                // bind the queue to the exchange
                ch.bindQueue(q.queue, process.env.RABBITMQ_WORKER_EXCHANGE, '');
                // consume from the queue, one message at a time.
                ch.consume(q.queue, function (msg) {
                    console.log("Message received: %s", msg.content.toString());
                    //save message to db
                    db.store(msg.content.toString()).then(function() {
                        //acknowledge receipt of message to amqp
                        console.log("Acknowledging message");
                        ch.ack(msg, true);
                    });
                }, {noAck: false});
            });
        });
    });