<br><br><div class="gmail_quote">On Sat, Jan 22, 2011 at 2:46 AM, Michael Bridgen <span dir="ltr">&lt;<a href="mailto:mikeb@rabbitmq.com">mikeb@rabbitmq.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi Dan,<div><div></div><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I am new to rabbitmq. I got the python &quot;getting started&quot; examples to<br>
work just fine.<br>
I would like to be able to send messages between node.js (using<br>
node.amqp) and python/pika.<br>
</blockquote>
&gt;<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I can successfully receive a message in node.js using this code:<br>
<br>
[receiver - node.js]<br>
var sys = require(&#39;sys&#39;);<br>
var amqp = require(&#39;./amqp&#39;);<br>
<br>
var connection = amqp.createConnection({ host: &#39;localhost&#39; });<br>
<br>
// Wait for connection to become established.<br>
connection.addListener(&#39;ready&#39;, function () {<br>
 � // Create a queue and bind to all messages.<br>
 � // Use the default &#39;amq.topic&#39; exchange<br>
<br>
 � var q = connection.queue(&#39;my-queue&#39;);<br>
 � // Catch all messages<br>
 � q.bind(&#39;#&#39;);<br>
<br>
 � // Receive messages<br>
 � q.subscribe(function (message) {<br>
 � � // Print messages to stdout<br>
 � � sys.puts(sys.inspect(message));<br>
 � });<br>
});<br>
<br>
[sender - python/pika]<br>
#!/usr/bin/env python<br>
import pika<br>
<br>
connection = pika.AsyncoreConnection(pika.ConnectionParameters(<br>
 � � � � host=&#39;localhost&#39;))<br>
channel = connection.channel()<br>
<br>
<br>
channel.basic_publish(exchange=&#39;&#39;,<br>
 � � � � � � � � � � � routing_key=&#39;my-queue&#39;,<br>
 � � � � � � � � � � � body=&#39;Hello World!&#39;)<br>
print &quot; [x] Sent &#39;Hello World!&#39;&quot;<br>
connection.close()<br>
</blockquote>
<br></div></div>
You should be aware that node-amqp, at least Ryan&#39;s original and most forks of it, make some unorthodox choices for parameter defaults and behaviour, which to be fair are probably inherited from the EventMachine AMQP client.<br>

<br>
In particular, the exchange that&#39;s published or bound to, if none is given, is &quot;amq.topic&quot;. �This is directly at odds with AMQP, for which an empty exchange name means the &quot;default exchange&quot; defined in the specification; that is, route directly to the queue named in the routing key.<br>

<br>
So your example above is working by coincidence -- you don&#39;t need to bind the queue in the node.js code, since your Python code is effectively sending straight to the queue. �I.e., the node.js code sets up<br>
<br>
(amq.topic) --&quot;#&quot;--&gt; [ | | my-queue | | ]<br>
<br>
but the message sent in the Python code actually follows<br>
<br>
(default) --&quot;my-queue&quot;--&gt; [ | | my-queue | | ]<div><div></div><div class="h5"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
But I can&#39;t go the other way. I&#39;m not sure that the message sent by<br>
node.js is going anywhere. I&#39;ve looked at the unit tests for node.amqp<br>
(they all pass) but there is something I am missing, probably to do with<br>
exchanges or something. I&#39;d like to do something like this:<br>
<br>
[sender - node.js]<br>
var sys = require(&#39;sys&#39;);<br>
var amqp = require(&#39;./amqp&#39;);<br>
<br>
var connection = amqp.createConnection({ host: &#39;localhost&#39; });<br>
<br>
// Wait for connection to become established.<br>
connection.addListener(&#39;ready&#39;, function () {<br>
 � // Create a queue and bind to all messages.<br>
 � // Use the default &#39;amq.topic&#39; exchange<br>
 � connection.publish(&quot;my-queue&quot;, {random_key:&quot;this is my message&quot;});<br>
 � connection.end();<br>
});<br>
<br>
[receiver - python/pika]<br>
#!/usr/bin/env python<br>
import pika<br>
import sys<br>
<br>
connection = pika.AsyncoreConnection(pika.ConnectionParameters(<br>
 � � � � host=&#39;localhost&#39;))<br>
channel = connection.channel()<br>
<br>
print &#39; [*] Waiting for messages. To exit press CTRL+C&#39;<br>
<br>
def callback(ch, method, properties, body):<br>
 � � print body,<br>
 � � sys.stdout.flush()<br>
channel.basic_consume(callback,<br>
 � � � � � � � � � � � queue=&#39;my-queue&#39;,<br>
 � � � � � � � � � � � no_ack=True)<br>
<br>
pika.asyncore_loop()<br>
</blockquote>
<br></div></div>
This way around (presuming there&#39;s a restart in-between runs), node.js sends to amq.topic, but nothing is bound there, so the message is discarded.<br>
<br>
You might try binding the queue, in the Python, to the exchange &quot;amq.topic&quot;. �You should probably also declare it.<br>
<br>
If you actually don&#39;t want to use &quot;amq.topic&quot;, and you probably don&#39;t if you can help it, you might declare your own exchange and use that.<br>
<br></blockquote><div><br></div><div>OK, I am trying this approach, with the following sender:</div><div><br></div><div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">var sys = require(&#39;sys&#39;);</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">var amqp = require(&#39;./amqp&#39;);</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div>
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">var connection = amqp.createConnection({ host: &#39;localhost&#39; });</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">// Wait for connection to become established.</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">connection.addListener(&#39;ready&#39;, function () {</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��// Create a queue and bind to all messages.</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��// Use the default &#39;amq.topic&#39; exchange</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��var exchange = connection.exchange(&quot;my-exchange&quot;);</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��exchange.publish(&quot;my-queue&quot;, {random_key:&quot;this is my message&quot;});</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��connection.end();</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">});</font></div>
<div><br></div><div>and receiver:<br><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">#!/usr/bin/env python</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">import pika</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">import sys</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>
</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">connection = pika.AsyncoreConnection(pika.ConnectionParameters(</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� � � �host=&#39;localhost&#39;))</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">channel = connection.channel()</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">result = channel.queue_declare(&#39;my-queue&#39;)</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">channel.exchange_declare(exchange=&#39;my-exchange&#39;)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">channel.queue_bind(exchange=&#39;my-exchange&#39;, queue=result.queue)</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">print &#39; [*] Waiting for messages. To exit press CTRL+C&#39;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">def callback(ch, method, properties, body):</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� �print body,</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� �sys.stdout.flush()</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� �</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">channel.basic_consume(callback,</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� � � � � � � � � � �queue=&#39;my-queue&#39;,</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� � � � � � � � � � �no_ack=True)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div>
<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">pika.asyncore_loop()</font></div><div><br></div><div>When I run the receiver, I get:</div><div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">% python receiver.py�</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Traceback (most recent call last):</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;receiver.py&quot;, line 9, in &lt;module&gt;</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">�� �result = channel.queue_declare(&#39;my-queue&#39;)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/spec.py&quot;, line 3003, in queue_declare</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/channel.py&quot;, line 187, in _rpc</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/connection.py&quot;, line 325, in _rpc</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/connection.py&quot;, line 301, in send_method</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/connection.py&quot;, line 295, in send_frame</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/codec.py&quot;, line 74, in marshal</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">��File &quot;build/bdist.macosx-10.6-universal/egg/pika/spec.py&quot;, line 673, in encode</font></div>
<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">TypeError: unsupported operand type(s) for &amp;: &#39;str&#39; and &#39;long&#39;�</font></div><div><br></div><div>What am I doing wrong?</div><div>
Thanks very much for your help.</div><div>Dan</div><div><br></div></div>