Working with Bindings
1. Creating Queue Binding
| Property | Description | Example |
|---|---|---|
| queue | Name of the queue to bind | my-queue |
| exchange | Name of the exchange to bind to | my-exchange |
| routingKey | Routing key for the binding (optional) | logs.info |
Example: Bind a queue to an exchange
const amqp = require('amqplib');
async function bindQueue() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'my-queue';
const exchange = 'my-exchange';
await channel.bindQueue(queue, exchange, 'logs.info');
console.log('Queue bound to exchange');
await channel.close();
await connection.close();
}
bindQueue().catch(console.error);
2. Using Routing Keys
| Property | Description | Values |
|---|---|---|
| routing_key | Used to route messages to queues based on message content | string |
| pattern | Pattern used for matching routing keys in topic exchanges | string |
Example: Publishing with Routing Key
RabbitMQ.publish(
'my-topic-exchange',
'user.created',
JSON.stringify({ userId: 12345, action: 'created' })
);
Example: Binding Queue with Routing Key
RabbitMQ.bindQueue(
'user-queue',
'my-topic-exchange',
'user.*'
);
Note: The
routing_key is used in direct and topic exchanges to determine which queues receive the message.
Warning: In topic exchanges,
* matches one word and # matches zero or more words.
3. Binding with Direct Exchange
| Property | Description | Value |
|---|---|---|
| exchange.name | Name of the direct exchange | direct_exchange |
| routing.key | Routing key used for binding | user.created |
| queue.name | Name of the queue to bind | user_queue |
Example: Binding a Queue to Direct Exchange
const amqp = require('amqplib');
async function bindQueueToDirectExchange() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchangeName = 'direct_exchange';
const queueName = 'user_queue';
const routingKey = 'user.created';
await channel.assertExchange(exchangeName, 'direct', { durable: true });
await channel.assertQueue(queueName, { durable: true });
await channel.bindQueue(queueName, exchangeName, routingKey);
console.log(`Bound queue '${queueName}' to exchange '${exchangeName}' with routing key '${routingKey}'`);
await channel.close();
await connection.close();
}
bindQueueToDirectExchange().catch(console.error);
Note: Direct exchanges route messages based on the routing key. A message sent with a specific routing key will only be delivered to queues that are bound with the same routing key.
4. Binding with Topic Exchange
| Property | Description | Values |
|---|---|---|
| exchange.name | Name of the topic exchange | my.topic.exchange |
| routing.key | Pattern used to bind queue to exchange | logs.# |
| queue.name | Name of the queue to bind | error.queue |
Example: Binding a Queue to a Topic Exchange
const amqp = require('amqplib');
async function bindQueueToTopicExchange() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchangeName = 'my.topic.exchange';
const queueName = 'error.queue';
const routingKey = 'logs.#';
await channel.assertExchange(exchangeName, 'topic');
await channel.assertQueue(queueName);
await channel.bindQueue(queueName, exchangeName, routingKey);
console.log('Queue bound to topic exchange with routing key:', routingKey);
}
bindQueueToTopicExchange().catch(console.error);
Note: The
# wildcard matches zero or more words in the routing key.
Warning: Ensure that the exchange is declared before binding queues to it.
| Routing Key Pattern | Matches | Description |
|---|---|---|
logs.# |
logs.info, logs.error, logs.debug.level1 |
Matches any message with a routing key starting with logs. |
logs.* |
logs.info, logs.error |
Matches any message with a routing key starting with logs. and exactly one word after it |
logs.*.level* |
logs.debug.level1, logs.info.level2 |
Matches any message with a routing key starting with logs., followed by one word, and then any string |
5. Binding with Fanout Exchange
| Property | Description | Value |
|---|---|---|
| exchange.type | Type of exchange | fanout |
| exchange.name | Name of the exchange | myFanoutExchange |
| queue.name | Name of the queue to bind | myQueue |
| routing.key | Routing key for binding (optional for fanout) | |
Example: Binding a Queue to a Fanout Exchange
const amqp = require('amqplib');
async function bindFanoutExchange() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchangeName = 'myFanoutExchange';
const queueName = 'myQueue';
await channel.assertExchange(exchangeName, 'fanout', { durable: true });
await channel.assertQueue(queueName, { durable: true });
await channel.bindQueue(queueName, exchangeName, '');
console.log('Queue bound to fanout exchange');
await channel.close();
await connection.close();
}
bindFanoutExchange().catch(console.error);
Note: In a fanout exchange, all messages are delivered to all queues bound to it. The routing key is ignored.
6. Binding with Headers Exchange
| Property | Value | Description |
|---|---|---|
| exchange.name | my-headers-exchange |
Name of the headers exchange to bind to |
| routing.key | my-routing-key |
Routing key used for binding (optional) |
| headers | { "priority": "high", "type": "notification" } |
Map of header names and values to match |
| operator | all |
Matching operator: all or any |
Example: Binding Headers Exchange
const amqp = require('amqplib');
async function bindHeadersExchange() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchangeName = 'my-headers-exchange';
const queueName = 'my-queue';
await channel.assertExchange(exchangeName, 'headers');
await channel.assertQueue(queueName);
const headers = { priority: 'high', type: 'notification' };
await channel.bindQueue(queueName, exchangeName, '', headers);
console.log('Headers exchange bound to queue');
}
bindHeadersExchange().catch(console.error);
Note: The
operator parameter can be set to all or any to control how header matches are evaluated.
Warning: Header matching is case-sensitive. Ensure header keys and values match exactly.
7. Creating Exchange-to-Exchange Binding
| Property | Description | Value |
|---|---|---|
| source | The name of the source exchange | source_exchange |
| destination | The name of the destination exchange | destination_exchange |
| routing_key | The routing key to use for the binding | routing_key |
| arguments | Optional arguments for the binding | {} |
Example: Creating Exchange-to-Exchange Binding
const amqp = require('amqplib');
async function createExchangeBinding() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const sourceExchange = 'source_exchange';
const destinationExchange = 'destination_exchange';
const routingKey = 'routing_key';
await channel.bindExchange(destinationExchange, sourceExchange, routingKey);
console.log('Exchange binding created successfully');
await channel.close();
await connection.close();
}
createExchangeBinding().catch(console.error);
8. Using Multiple Bindings
Bindings in RabbitMQ can be combined to create complex routing topologies. Multiple bindings can exist between the same exchange and queue, allowing for flexible message distribution.
| Binding Key | Exchange Type | Description |
|---|---|---|
| routing.key1 | direct |
Routes messages to queues with matching routing keys |
| routing.key2 | direct |
Routes messages to queues with matching routing keys |
| routing.# | topic |
Routes messages to queues with matching patterns |
Example: Creating Multiple Bindings
const amqp = require('amqplib');
async function setupMultipleBindings() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchangeName = 'myExchange';
const queueName = 'myQueue';
await channel.assertExchange(exchangeName, 'direct');
await channel.assertQueue(queueName);
// Bind queue to exchange with different routing keys
await channel.bindQueue(queueName, exchangeName, 'routing.key1');
await channel.bindQueue(queueName, exchangeName, 'routing.key2');
console.log('Multiple bindings created successfully');
}
setupMultipleBindings().catch(console.error);
Note: Multiple bindings can be used to implement fan-out or conditional routing logic.
Summary of Multiple Bindings
- Each binding defines a route from an exchange to a queue
- Multiple bindings can exist between the same exchange and queue
- Bindings can use different routing keys or patterns
9. Understanding Binding Arguments
| Argument | Description | Values | Notes |
|---|---|---|---|
| routing_key | Used to route messages to queues | string |
Required for direct and topic exchanges |
| source | Exchange name that is the source of the binding | string |
Required for exchange-to-exchange bindings |
| destination | Queue or exchange name that is the destination of the binding | string |
Required for exchange-to-exchange bindings |
| destination_type | Type of the destination (queue or exchange) | queue, exchange |
Required for exchange-to-exchange bindings |
| headers | Map of headers to match for message routing | map |
Used in headers exchange type |
| priority | Binding priority for use with priority queues | integer |
Used in priority queue exchange type |
Example: Binding with routing key
const bindingOptions = {
routingKey: 'order.created',
source: 'orders-exchange',
destination: 'order-queue'
};
Example: Binding with headers
const bindingOptions = {
headers: {
type: 'order',
priority: 'high'
},
source: 'notifications-exchange',
destination: 'high-priority-queue'
};
10. Unbinding Queue
Unbinding a queue from an exchange removes the binding between them, which stops the queue from receiving messages that match the binding key.
| Parameter | Description | Required |
|---|---|---|
| queue | Name of the queue to unbind | Yes |
| exchange | Name of the exchange to unbind from | Yes |
| routingKey | Routing key used in the binding (optional) | No |
Example: Unbind Queue from Exchange
const amqp = require('amqplib');
async function unbindQueue() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queueName = 'myQueue';
const exchangeName = 'myExchange';
const routingKey = 'my.routing.key';
await channel.unbindQueue(queueName, exchangeName, routingKey);
console.log('Queue unbound from exchange');
await channel.close();
await connection.close();
}
unbindQueue().catch(console.error);
Note: If
routingKey is not provided, all bindings between the queue and exchange will be removed.
11. Unbinding Exchange
| Property | Value | Description |
|---|---|---|
| exchange.name | my-exchange |
Name of the exchange to unbind from |
| routing.key | my.routing.key |
Routing key used in the binding |
| queue.name | my-queue |
Name of the queue to unbind from |
Example: Unbind Exchange from Queue
const amqp = require('amqplib');
async function unbindExchangeFromQueue() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const exchangeName = 'my-exchange';
const queueName = 'my-queue';
const routingKey = 'my.routing.key';
await channel.unbindExchange(exchangeName, queueName, routingKey);
console.log('Exchange unbound from queue successfully');
await channel.close();
await connection.close();
}
unbindExchangeFromQueue().catch(console.error);
12. Listing Bindings
| Property | Description | Values |
|---|---|---|
| source | Name of the exchange | string |
| destination | Name of the queue or exchange | string |
| routing_key | Routing key used for binding | string |
| destination_type | Type of destination (queue or exchange) | queue or exchange |