使用symfony rabbitMq通信nodeJs

时间:2017-02-13 11:11:38

标签: node.js docker rabbitmq symfony

Node Js:我创建了一个带有nodejs和socket的用户,用于与之通信 rabbitMq和symfony

file:consumer.js

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


amqp.connect('amqp://user:pass@192.168.99.10:55672', 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});
 });
});

文件:App.js

 var server = require('http').createServer();
 var io = require('socket.io')(server);

io.on('connection', function(client){
console.log('here');
client.on('event', function(data){});
client.on('disconnect', function(){});
 });
server.listen(3000);

Symfony 3

我用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}

现在问题是我不知道如何与生产者和消费者沟通。 node consumer.js执行normal并等待消息 node app.js它没有显示任何内容

1 个答案:

答案 0 :(得分:0)

看起来你可能没有发布到同一个队列。

在进一步试验之前,我会使用docs, exactly as they are同时发送和接收PHP和Javascript。