{"action":"created","issue":{"url":"https://api.github.com/repos/pdezwart/php-amqp/issues/155","labels_url":"https://api.github.com/repos/pdezwart/php-amqp/issues/155/labels{/name}","comments_url":"https://api.github.com/repos/pdezwart/php-amqp/issues/155/comments","events_url":"https://api.github.com/repos/pdezwart/php-amqp/issues/155/events","html_url":"https://github.com/pdezwart/php-amqp/issues/155","id":65186727,"number":155,"title":"RabbitMQ's Direct reply-to","user":{"login":"anton-shirikov","id":2010311,"avatar_url":"https://avatars.githubusercontent.com/u/2010311?v=3","gravatar_id":"","url":"https://api.github.com/users/anton-shirikov","html_url":"https://github.com/anton-shirikov","followers_url":"https://api.github.com/users/anton-shirikov/followers","following_url":"https://api.github.com/users/anton-shirikov/following{/other_user}","gists_url":"https://api.github.com/users/anton-shirikov/gists{/gist_id}","starred_url":"https://api.github.com/users/anton-shirikov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/anton-shirikov/subscriptions","organizations_url":"https://api.github.com/users/anton-shirikov/orgs","repos_url":"https://api.github.com/users/anton-shirikov/repos","events_url":"https://api.github.com/users/anton-shirikov/events{/privacy}","received_events_url":"https://api.github.com/users/anton-shirikov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2015-03-30T10:56:22Z","updated_at":"2015-04-10T18:20:29Z","closed_at":null,"body":"I've been trying to implement RabbitMQ's [direct reply-to](https://www.rabbitmq.com/direct-reply-to.html) and there seems to be a problem.\r\n\r\n#### First attempt:\r\n```php\r\n// Establish connection, create a channel & exchange for publishing...\r\n$replyQueue = new AMQPQueue($channel);\r\n$replyQueue->setName('amq.rabbitmq.reply-to');\r\n\r\necho 'Publishing...' . PHP_EOL;\r\n$exchange->publish('ping', null, AMQP_NOPARAM, [\r\n    'reply_to' => 'amq.rabbitmq.reply-to',\r\n]);\r\n\r\necho 'Waiting for reply...' . PHP_EOL;\r\n$replyQueue->consume(function (AMQPEnvelope $envelope) {\r\n   echo $envelope->getBody() . PHP_EOL;\r\n}, AMQP_AUTOACK);\r\n```\r\n\r\nWith amqp 1.4.0 the producer hangs on consume and rabbitmq logs contain this error:\r\n```\r\nChannel error on connection <0.601.4> (172.17.0.105:48186 -> 172.17.0.103:5672, vhost: '/', user: 'admin'), channel 1:\r\n{amqp_error,precondition_failed,\"fast reply consumer does not exist\",\r\n            'basic.publish'}\r\n```\r\nWith amqp 1.6.0beta2 producer recognizes the error and throws exception:\r\n```\r\nPHP Fatal error:  Uncaught exception 'AMQPQueueException' with message 'Server channel error: 406, message: PRECONDITION_FAILED - fast reply consumer does not exist' in /var/code/producer.php:26\r\n```\r\n\r\n#### Second attempt (kinda working):\r\n\r\nJudging from this reply http://permalink.gmane.org/gmane.comp.networking.rabbitmq.general/36792\r\nI need to first consume from `amq.rabbitmq.reply-to`  before i send a request.\r\n\r\nI've tried to find a workaround with amqp-1.6.0beta2's consume from multiple queues and it is working. The problem is I have to declare an extra \"useless\" queue:\r\n\r\n```php\r\n// Establish connection, create a channel & exchange for publishing...\r\n$queue = new AMQPQueue($channel);\r\n$queue->setName('amq.rabbitmq.reply-to');\r\n$queue->consume(null, AMQP_AUTOACK);\r\n\r\n$queue2 = new AMQPQueue($channel);\r\n$queue2->setName('temp-queue');\r\n$queue2->declareQueue();\r\n\r\necho 'Publishing...' . PHP_EOL;\r\n$exchange->publish('ping', null, AMQP_NOPARAM, [\r\n    'reply_to' => 'amq.rabbitmq.reply-to',\r\n]);\r\n\r\necho 'Waiting for reply...' . PHP_EOL;\r\n$queue2->consume(function (AMQPEnvelope $envelope) {\r\n   echo $envelope->getBody() . PHP_EOL;\r\n}, AMQP_AUTOACK);\r\n\r\n$queue->cancel();\r\n$queue2->cancel();\r\n```\r\n\r\n#### php-amqplib\r\n\r\nWith php-amqplib library there seems to be no problem and everything is working as expected:\r\n\r\n```php\r\n// Establish connection, create a channel...\r\n$callback_queue = 'amq.rabbitmq.reply-to';\r\n\r\n$response = null;\r\n$channel->basic_consume($callback_queue, '', false, true, false, false, function (AMQPMessage $message) use (&$response) {\r\n    echo $message->body . PHP_EOL;\r\n    $response = true;\r\n});\r\n\r\n$msg = new AMQPMessage('ping', [\r\n    'reply_to' => $callback_queue\r\n]);\r\n\r\n$channel->basic_publish($msg, 'exchange_name');\r\nwhile (!$response) {\r\n    $channel->wait();\r\n}\r\n```\r\n\r\n#### Consumer\r\nConsumer seems to be irrelevant. I've used the same script for all 3 cases:\r\n\r\n```php\r\n$exchange = new AMQPExchange($channel);\r\n$exchange->setName('exchange_name');\r\n$exchange->setType(AMQP_EX_TYPE_DIRECT);\r\n$exchange->declareExchange();\r\n\r\n$queue = new AMQPQueue($channel);\r\n$queue->setName('queue');\r\n$queue->setFlags(AMQP_NOPARAM);\r\n$queue->declareQueue();\r\n$queue->bind($exchange->getName());\r\n\r\n$defaultExchange = new AMQPExchange($channel);\r\n\r\necho \"Waiting for messages...\" . PHP_EOL;\r\n$queue->consume(function (AMQPEnvelope $envelope) use ($queue, $defaultExchange) {\r\n    printf(\"Got message `%s` with reply-to `%s`\" . PHP_EOL, $envelope->getBody(), $envelope->getReplyTo());\r\n    $defaultExchange->publish('pong', $envelope->getReplyTo());\r\n}, AMQP_AUTOACK);\r\n```"},"comment":{"url":"https://api.github.com/repos/pdezwart/php-amqp/issues/comments/91643775","html_url":"https://github.com/pdezwart/php-amqp/issues/155#issuecomment-91643775","issue_url":"https://api.github.com/repos/pdezwart/php-amqp/issues/155","id":91643775,"user":{"login":"pinepain","id":2185793,"avatar_url":"https://avatars.githubusercontent.com/u/2185793?v=3","gravatar_id":"","url":"https://api.github.com/users/pinepain","html_url":"https://github.com/pinepain","followers_url":"https://api.github.com/users/pinepain/followers","following_url":"https://api.github.com/users/pinepain/following{/other_user}","gists_url":"https://api.github.com/users/pinepain/gists{/gist_id}","starred_url":"https://api.github.com/users/pinepain/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pinepain/subscriptions","organizations_url":"https://api.github.com/users/pinepain/orgs","repos_url":"https://api.github.com/users/pinepain/repos","events_url":"https://api.github.com/users/pinepain/events{/privacy}","received_events_url":"https://api.github.com/users/pinepain/received_events","type":"User","site_admin":false},"created_at":"2015-04-10T18:20:29Z","updated_at":"2015-04-10T18:20:29Z","body":"You're welcome. Check the latest version now, there was dumb mistakes which should be fixed now (tests are green)."}}