NodejJS,RabbitMQ,客户端

时间:2018-07-20 10:33:56

标签: node.js rabbitmq

我正在尝试在客户端(在浏览器中)的NodeJS上使用RabbitMQ。 NodeJS我仅用于开发,但用于生产 服务器将部署在Django Python Web Framework上。

我尝试按照以下教程进行制作:

https://github.com/squaremo/node-amqp

https://github.com/squaremo/rabbit.js/blob/master/README.md

https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html

但是不幸的是,每次我得到错误时,都会发现 “连接”不起作用。

这让我厌倦了这种解决方案:

1。

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

amqp.connect('amqp://localhost', function(err, conn) {
  conn.createChannel(function(err, ch) {
    var q = 'hello';

    ch.assertQueue(q, {durable: false});
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch.consume(q, function(msg) {
      console.log(" [x] Received %s", msg.content.toString());
    }, {noAck: true});
  }); 
});

2。

var context = require('rabbit.js').createContext();
context.on('ready', function() {
  var pub = context.socket('PUB'), sub = context.socket('SUB');
  sub.pipe(process.stdout);
  sub.connect('events', function() {
    pub.connect('events', function() {
      pub.write(JSON.stringify({welcome: 'rabbit.js'}), 'utf8');
    });
  });
});

我收到这样的错误:

[Vue warn]: Error in created hook: "TypeError: 
    sock = __webpack_require__("./node_modules/node-libs-browser/mock/empty.js").connect(sockopts, onConnect); 
is not a function"

* empty.js显然是空白。

当我尝试这种方式时:

3。

var sys = require('sys');
var amqp = require('amqp');

var connection = amqp.createConnection({ host: 'dev.rabbitmq.com' });

// Wait for connection to become established.
connection.on('ready', function () {
  // Create a queue and bind to all messages.
  // Use the default 'amq.topic' exchange
  var q = connection.queue('my-queue');
  // Catch all messages
  q.bind('#');

  // Receive messages
  q.subscribe(function (message) {
    // Print messages to stdout
    sys.p(message);
  });
});

我收到这样的错误:

[Vue warn]: Error in created hook: "TypeError: 
    net.connect 
is not a function"

我尝试使用以下库:amqp,node-amqp,rabbit.js,amqplib

您有什么想法吗?

非常感谢您提供任何帮助,

Michał

0 个答案:

没有答案