2

I am writing a web app to interface with a RabbitMQ based software system. I'm new at web apps, but I've written a fair amount of the app using the MEAN stack and I'm happy with it. I found this link:

amqp vs amqplib - which Node.js amqp client library is better?

which suggest several good alternative libraries such as bramqp and amqp.node, but I have not been able to find the answer the following question. Which, if any, of these libraries allows me to interact with a header based exchange? Note that I would have been happy to post this as a follow up to the original link above, but I couldn't figure out how.

Howard

Community
  • 1
  • 1

1 Answers1

0

I have written a tutorial demonstrating the headers exchange below. Sometime soon there will be advanced tutorials like these added to bramqp. This should only print one of the messages.

var bramqp = require('bramqp');
var async = require('async');
var net = require('net');

var socket = net.connect({
    port : 5672
});
bramqp.initialize(socket, 'rabbitmq/full/amqp0-9-1.stripped.extended', function(error, handle) {
    async.series([ function(seriesCallback) {
        handle.openAMQPCommunication('guest', 'guest', true, seriesCallback);
    }, function(seriesCallback) {
        handle.queue.declare(1, 'header_test_queue');
        handle.once('1:queue.declare-ok', function(channel, method, data) {
            seriesCallback();
        });
    }, function(seriesCallback) {
        handle.basic.consume(1, 'header_test_queue', null, false, true, false, false, {});
        handle.once('1:basic.consume-ok', function(channel, method, data) {
            handle.on('1:basic.deliver', function(channel, method, data) {
                handle.once('content', function(channel, className, properties, content) {
                    console.log('got a message:');
                    console.log(content.toString());
                });
            });
            seriesCallback();
        });
    }, function(seriesCallback) {
        handle.exchange.declare(1, 'header_test_exchange', 'headers', function() {
            seriesCallback();
        });
    }, function(seriesCallback) {
        exchange_arguments = {
            'x-match': {
                type: 'Long string',
                data: 'all'
            },
            wizard : {
                type: 'Long string',
                data: 'magic'
            }
        };
        handle.queue.bind(1, 'header_test_queue', 'header_test_exchange', null, false, exchange_arguments, function() {
            seriesCallback();
        });
    }, function(seriesCallback) {
        handle.basic.publish(1, 'header_test_exchange', null, false, false, function() {
            properties = {
                headers: {
                    wizard : {
                        type: 'Long string',
                        data: 'magic'
                    }
                }
            };
            handle.content(1, 'basic', properties, 'Hello World! with magic', seriesCallback);
        });
    }, function(seriesCallback) {
        handle.basic.publish(1, 'header_test_exchange', null, false, false, function() {
            properties = {
                headers: {
                    wizard : {
                        type: 'Long string',
                        data: 'not magic'
                    }
                }
            };
            handle.content(1, 'basic', properties, 'Hello World! without magic', seriesCallback);
        });
    }, function(seriesCallback) {
        setTimeout(function() {
            handle.closeAMQPCommunication(seriesCallback);
        }, 10 * 1000);
    }, function(seriesCallback) {
        handle.socket.end();
        setImmediate(seriesCallback);
    } ], function() {
        console.log('all done');
    });
});
bakkerthehacker
  • 501
  • 3
  • 4