Wednesday, 31 May 2017

Can I use amqplib in the browser?

I am an AMQP/RabbitMQ newbie, and relative Node.js newbie. Can I use the amqplib NPM library client-side?

I'd like to be able to push messages direct to RabbitMQ from my Angular app. I have used Browserify to modularise a lot of my client-side code. I am now starting to experiment with RabbitMQ and would like to push messages direct from the browser to cloud-based queues over the amqp protocol.

I have installed amqplib via NPM and written/pasted the following module:

var amqp = require('amqplib/callback_api');

var push = function(){
    console.log('This is the CORE queue.pusher push function being triggered');

    var connString = 'amqp://username:pwd@blabla.rmq.cloudamqp.com/username';

    amqp.connect(connString, function(err, conn) {

        if (err){
            console.log("core queue.pusher push error %s", err);
        }else {
            conn.createChannel(function (err, ch) {
                var q = 'FatController';
                var msg = 'Hello World!';

                ch.assertQueue(q, {durable: false});
                // Note: on Node 6 Buffer.from(msg) should be used
                ch.sendToQueue(q, new Buffer(msg));
                console.log(" [x] Sent %s", msg);
            });

            setTimeout(function () {
                conn.close();
                process.exit(0)
            }, 500);
        }
    });

};

module.exports = {push:push};

When I try running this I get the following error:

bundle.js:32074 TypeError: QS.unescape is not a function
    at openFrames (bundle.js:9551)
    at connect (bundle.js:9629)
    at Object.connect (bundle.js:7959)
    at Object.push (bundle.js:7652)
    at controller.pushQueueEntry (bundle.js:7805)
    at fn (eval at compile (bundle.js:32907), <anonymous>:4:184)
    at callback (bundle.js:44543)
    at Scope.$eval (bundle.js:35710)
    at Scope.$apply (bundle.js:35810)
    at HTMLInputElement.<anonymous> (bundle.js:44548)
    at defaultHandlerWrapper (bundle.js:21283)
    at HTMLInputElement.eventHandler (bundle.js:21271)

Am I barking up the wrong tree here? Will amqplib only run in a 'proper' node environment?

As a secondary question, what is the best way to determine whether a particular NPM package will run in the browser environment? It seems to me that some NPM packages will run in the browser, and some won't - what is the best way to be confident about this?



via Journeyman

No comments:

Post a Comment