我是 AMQP/RabbitMQ 新手,也是相对的 Node.js 新手。我可以在客户端使用 amqplib NPM 库吗?
我希望能够从我的 Angular 应用程序将消息直接推送到 RabbitMQ。我使用 Browserify 来模块化我的很多客户端代码。我现在开始尝试使用 RabbitMQ,并希望通过 amqp 协议将消息直接从浏览器推送到基于云的队列。
我已经通过 NPM 安装了 amqplib 并编写/粘贴了以下模块:
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};
当我尝试运行它时,我收到以下错误:
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)
我在这里吠错树了吗?amqplib 是否只能在“正确”的节点环境中运行?
作为第二个问题,确定特定 NPM 包是否会在浏览器环境中运行的最佳方法是什么?在我看来,有些 NPM 包会在浏览器中运行,而有些则不会——对此有信心的最好方法是什么?