1

I am using Node.js with node-amqp to create a simple message queue. Most examples that I see do the following:

  1. Create a connection
  2. Create an exchange
  3. Creat a Queue and Bind it to the Exchange
  4. Publish via the Exchange

In my code, I omit the queue (step 3) since it is not used for publishing.

var _connection = amqp.createConnection(_options);

_connection.on('ready', function() {

    _connection.exchange('myexchange', { type: 'direct', autoDelete: false }, function(ex) {

        ex.publish({hello:'world'});
    });
});

Is this ok? or is there a reason for the queue?

Mike M
  • 4,879
  • 5
  • 38
  • 58

2 Answers2

2

There is nothing wrong with the code that you have. This is a good example of how you can keep your message producer nice and simple / clean.

However, the code you've shown is only half of the messaging solution. You need both a message producer, as shown, and a message consumer.

A Message Consumer

The message consumer is the code that does the real work. It receives a message from a queue to which it is subscribed, and processes that message however you tell it to.

That's the key, here - a message consumer will consume a message from a queue. If you want to send a message and have it be processed, then, you must have a queue for the message.

The Postal System Analogy

Think of this like this:

When you write a letter (pen and paper), you put it in an envelope. Then you write an address on the envelope and send it through your postal system. The postal system knows what the address means, sends it through various trucks and mail processing centers, and eventually puts it in a mailbox for the recipient.

It's the same thing with messaging in RabbitMQ.

You are delivering a letter to a destination. You write an "address" (exchange name, and routing key) on the message and RabbitMQ figures out how to deliver it to the appropriate place.

With physical mail, your letter is put in a mailbox for someone to read. With RabbitMQ and messaging, your message it put in a queue for some software to read.

You need a queue for the software to receive the message and process it.

...

P.S. If you're in need of some ground-up materials on RabbitMQ and NodeJS, check out my RabbitMQ For Developers package. It will get you up and running in no time, with the most common RMQ questions and patterns.

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
0

Queues are explicitly created and bound to exchange to make sure published message will not be lost in case no queue(s) and bindings previously exists.

In RabbitMQ most operations about entities creations (exchanges, queues, bindings) are idempotent. It means that if you call them more than once with the same arguments, they will provide the same result as called once.

In case of exchange, you can't publish to nonexistent exchange (channel-level AMQP exception occurs), but if no proper queues and bindings exist for particular message, it will be lost (or dead-lettered, see Dead Letter Exchanges and Alternate Exchanges for more).

pinepain
  • 12,453
  • 3
  • 60
  • 65
  • Is this not what the 'autoDelete':false prevents? – Mike M Nov 01 '15 at 09:47
  • No, [`auto-delete`](https://www.rabbitmq.com/amqp-0-9-1-reference.html#queue.declare.auto-delete) flag controls whether queue (or exchange) is not in use anymore (for queue it means that queue had at least one consumer before, but now all of them are finished or canceled) and thus should be deleted automatically. – pinepain Nov 01 '15 at 12:06
  • Sorry: the node-amqp docs say : "autoDelete: boolean, default true. If set, the exchange is deleted when there are no longer queues bound to it." So that means that if I set it to true, the exchange will remain open even with no queues attached to it. Correct? Is this is so, why do I need to bind a Q for producing messages only? – Mike M Nov 01 '15 at 13:11
  • `auto-delete` comes from AMQP standard and I would recommend to refer to RabbitMQ docs about [`queue.declare(auto-delete)`](https://www.rabbitmq.com/amqp-0-9-1-reference.html#queue.declare.auto-delete) and [`exchange.declare(auto-delete)`](https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.auto-delete) flags for details. – pinepain Nov 01 '15 at 15:15