Rabbitmq生产者(symfony 3)和消费者错误(NodeJs)

时间:2017-02-13 08:48:04

标签: node.js symfony docker rabbitmq

我用symfony 3创建了一个Producer

ProducerController

class ProducerController extends Controller
{

/**
* @Route("/producer", name="producer")
*/
 public function uploadAction(Request $request)
 {

$userId =  mt_rand(1, 1000000);

$message = \Swift_Message::newInstance()
    ->setSubject('Sujet de l\'email')
    ->setFrom('test@xxx.com')
    ->setTo('destinaire@xxx.com')
    ->setBody($this->renderView('index.html.twig', ['userId' =>$userId]), 'text/html');

$this->get('old_sound_rabbit_mq.message_producer')->publish(serialize($message));

return $this->render('welcome.html.twig', ['userId' => $userId]);

}

}

config.yml

 old_sound_rabbit_mq:
connections:
    default:
    host:     '192.168.99.10' # hostname and port of the rabbitmq server
    port:     55672
    user:     'user'
    password: 'pass'
    vhost:    '/'
    lazy:     true # a lazy connection avoids unnecessary connections to the broker on every request

producers:
message:
    connection:       default # connects to the default connection configured above
    exchange_options: {name: 'message', type: direct}

在我用symfony创建一个消费者之后,程序工作正常,但现在我想用NodeJs创建这个消费者

的NodeJS

consumer.js

 var http = require('http'),
 url = require('url'),
 amqp = require('amqplib');

 var rabbitMQ = amqp.createConnection({url: "amqp://user:pass@192.168.99.10:55672"},{defaultExchangeName: ''});


 rabbitMQ.addListener('ready', function(){
 var queue = rabbitMQ.queue('',{'exclusive':true}, function(q){

  //get all messages for the rabbitExchange
  q.bind('rabbitExchange','#');
  console.log("inqueue")
  // Receive messages
  q.subscribe(function (message) {
    // Print messages to stdout
    console.log("received message");
    console.log(message.data.toString());
  });
});

});

的package.json

{
  "name": "amqp_queue-service",
  "version": "1.0.0",
  "description": "RabbitMQ amqp.node",
  "main": "app.js",
  "author": "",
  "license": "MIT",
  "dependencies": {
  "amqplib": "^0.3.0",
  "socket.io": "^1.7.2"
  }
}

当我执行app.js时 它没有给我看任何东西

当我执行consumer.js时 它告诉我

var rabbitMQ = amqp.createConnection({url: "amqp://user:pass@192.168.99.10:55672"},{defaultExchangeName: ''});
                ^

TypeError: amqp.createConnection is not a function
at Object.<anonymous> (C:\Users\SOTHNYR\Desktop\NodeJS\consumer.js:7:21)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3

1 个答案:

答案 0 :(得分:1)

我认为应该如此 var rabbitMQ = amqp.connect("amqp://user:pass@192.168.99.10:55672"});

来自:http://www.squaremobius.net/amqp.node/channel_api.html#connect