[rabbitmq-discuss] Node.js & RabbitMQ

Michael Bridgen mikeb at rabbitmq.com
Mon Jun 10 16:59:45 BST 2013


> How do you correctly close a connection?

The official way, `connection.end`, closes the underlying socket. This 
means no more bytes hither or thither, so you'll want to make sure 
everything you wanted done has been done first; this is the main purpose 
of the amqp_hacks.safeCloseConnection from the tutorials.

> Why is my exchange not being created?

What exactly is the problem you're seeing? If I take the code below, 
stick it in a file, and run it, it runs to completion and logs all the 
given output; further, I can see the exchange in the management web UI, 
which also registers the message going through as a blip in the message 
rate.

```
var amqp = require('amqp');
var RABBITMQ_URL = 'amqp://localhost';

var connection = amqp.createConnection({url: RABBITMQ_URL});

connection.on('ready', function(){
   console.log("Connection established");
   connection.exchange('cloudrouter', {"type": "direct"}, 
function(exchange){
     console.log("Exchange created");
     exchange.publish('',"Hello World");
     console.log(" [x] Sent Hello World");
     connection.end();
   });
});
```

If you run the code in a REPL line-by-line, by the way, it will likely 
*not* execute the ready event handler (so nothing will happen). By the 
time the prompt returns for the next line, all the I/O is done and that 
one-off event has fired.

-Michael

> var connection = amqp.createConnection({url: RABBITMQ_URL});
>
> connection.on('ready', function(){
> //console.log("Connection established");
> connection.exchange('cloudrouter', {"type": "direct"}, function(exchange){
> console.log("Exchange created");
> console.log("Another Message");
> //exchange.publish('',"Hellow World");
> //console.log(" [x] Sent Hello World");
> //amqp_hacks.safeEndConnection(connection);
> });
> });
>
>
>
> --
> Warmest Regards,
>
> *Josh Birdwell*
> STEM Advocate
> Oklahoma City, Oklahoma
> Mobile: (405) 388-0176
> <http://www.twitter.com/jbirdwell34>
> <http://www.facebook.com/rfidedgellc>
> <http://www.linkedin.com/in/joshbirdwell> <http://www.rfidedge.org>
>
>
> _______________________________________________
> rabbitmq-discuss mailing list
> rabbitmq-discuss at lists.rabbitmq.com
> https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss
>



More information about the rabbitmq-discuss mailing list