2

I need to use Amazon MQ to consume and publish message to queue using amqp protocol in Nodejs. I have already set up AWS MQ, define the broker and created a queue.

I have followed AWS Javascript SDk but still I am not able to find any method to consume and publish message to queue.

Can someone help me how to connect to AWS MQ using amqp protocol to consume and publish message to queue.

Thanks

Ankit Uniyal
  • 424
  • 1
  • 7
  • 20

1 Answers1

1

I have used amqp10 npm module which works to consume and publish message to AWS MQ.

Below is the code :

  const AMQPClient = require('amqp10').Client;
  const Policy = require('amqp10').Policy;

1.To Consume message from AWS MQ :

      let client = new AMQPClient(Policy.Utils.RenewOnSettle(1, 1, 
Policy.ServiceBusQueue));
      let connectionString = 'your_connnection_string';
      client.connect(connectionString)
          .then(function() {
              console.log("Connected");
              return Promise.all([

client.createReceiver(configurationHolder.config.getMessageQueueName)
              ]);
          })
          .spread(function(receiver) {
                  receiver.on('errorReceived', function(rx_err) {
                      console.warn('===> RX ERROR: ', rx_err);
                      return err;
                  });
                  receiver.on('message', function(message) {
                      client.disconnect().then(function() {
                      console.log('disconnected, when we get the message from the queue);
                      return message.body;
                  });
              });
          })
          .error(function(e) {
                  console.warn('connection error: ', e);
                  return err;
              });
  1. To Publish Message to AWS MQ :

    let client = new AMQPClient(Policy.merge({
        senderLinkPolicy: {
            callbackPolicy: Policy.Utils.SenderCallbackPolicies.OnSent
        }
    }, Policy.DefaultPolicy));
    
    
        client.connect(connectionString, {
                'saslMechanism': 'ANONYMOUS'
            })
            .then(function() {
                console.log("Connected");
                return Promise.all([
                    client.createSender(queueName)
                ]);
            })
            .spread(function(sender) {
                sender.on('errorReceived', function(tx_err) {
                    console.warn('===> TX ERROR: ', tx_err);
                    return err;
                });
                var options = {
                    annotations: {
                        'x-opt-partition-key': 'pk' + msgValue
                    }
                };
                return sender.send(JSON.stringify(msgValue), 
    options).then(function(state) {
                    client.disconnect().then(function() {
                        console.log('disconnected, when we saw the value we 
    inserted after publish to AWS MQ.');
                        return state;
                    });
                });
            })
            .error(function(e) {
                console.warn('connection error: ', e);
                return err;
            });
    

Thanks

Ankit Uniyal
  • 424
  • 1
  • 7
  • 20
  • Hey Ankit have you tried this approach(library) with high load? Also I see you first connect and then create sender, but when you send millions of messages do you create connection and then senders all the time? – Shalva Kakauridze Sep 23 '18 at 07:19
  • Hi Shark, I create connection and sender (specific to queue) on the first message publish only. Although I didn't find any provision to publish bulk messages since it published each message one by one and it is definitely faster because I don't need to create connection and sender on every message. – Ankit Uniyal Sep 24 '18 at 07:13