Skip to content

Commit 1b8ceb3

Browse files
docs: update rmq docs, add wildcards section
1 parent d59d85f commit 1b8ceb3

File tree

1 file changed

+77
-2
lines changed

1 file changed

+77
-2
lines changed

content/microservices/rabbitmq.md

+77-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The `options` property is specific to the chosen transporter. The <strong>Rabbit
4848
<table>
4949
<tr>
5050
<td><code>urls</code></td>
51-
<td>Connection urls</td>
51+
<td>An array of connection URLs to try in order</td>
5252
</tr>
5353
<tr>
5454
<td><code>queue</code></td>
@@ -68,7 +68,7 @@ The `options` property is specific to the chosen transporter. The <strong>Rabbit
6868
</tr>
6969
<tr>
7070
<td><code>consumerTag</code></td>
71-
<td>Consumer Tag Identifier (read more <a href="https://amqp-node.github.io/amqplib/channel_api.html#channel_consume" rel="nofollow" target="_blank">here</a>)</td>
71+
<td>A name which the server will use to distinguish message deliveries for the consumer; mustn’t be already in use on the channel. It’s usually easier to omit this, in which case the server will create a random name and supply it in the reply. Consumer Tag Identifier (read more <a href="https://amqp-node.github.io/amqplib/channel_api.html#channel_consume" rel="nofollow" target="_blank">here</a>)</td>
7272
</tr>
7373
<tr>
7474
<td><code>queueOptions</code></td>
@@ -82,6 +82,38 @@ The `options` property is specific to the chosen transporter. The <strong>Rabbit
8282
<td><code>headers</code></td>
8383
<td>Headers to be sent along with every message</td>
8484
</tr>
85+
<tr>
86+
<td><code>replyQueue</code></td>
87+
<td>Reply queue for the producer. Default is <code>amq.rabbitmq.reply-to</code></td>
88+
</tr>
89+
<tr>
90+
<td><code>persistent</code></td>
91+
<td>If truthy, the message will survive broker restarts provided it’s in a queue that also survives restarts</td>
92+
</tr>
93+
<tr>
94+
<td><code>noAssert</code></td>
95+
<td>When false, a queue will not be asserted before consuming</td>
96+
</tr>
97+
<tr>
98+
<td><code>wildcards</code></td>
99+
<td>Set to true only if you want to use Topic Exchange for routing messages to queues. Enabling this will allow you to use wildcards (*, #) as message and event patterns</td>
100+
</tr>
101+
<tr>
102+
<td><code>exchange</code></td>
103+
<td>Name for the exchange. Defaults to the queue name when "wildcards" is set to true</td>
104+
</tr>
105+
<tr>
106+
<td><code>exchangeType</code></td>
107+
<td>Type of the exchange. Default is <code>topic</code>. Valid values are <code>direct</code>, <code>fanout</code>, <code>topic</code>, and <code>headers</code></td>
108+
</tr>
109+
<tr>
110+
<td><code>routingKey</code></td>
111+
<td>Additional routing key for the topic exchange</td>
112+
</tr>
113+
<tr>
114+
<td><code>maxConnectionAttempts</code></td>
115+
<td>Maximum number of connection attempts. Applies only to the consumer configuration. -1 === infinite</td>
116+
</tr>
85117
</table>
86118

87119
#### Client
@@ -300,3 +332,46 @@ Similarly, you can access the server's underlying driver instance:
300332
const managerRef =
301333
server.unwrap<import('amqp-connection-manager').AmqpConnectionManager>();
302334
```
335+
336+
#### Wildcards
337+
338+
RabbitMQ supports the use of wildcards in routing keys to allow for flexible message routing. The `#` wildcard matches zero or more words, while the `*` wildcard matches exactly one word.
339+
340+
For example, the routing key `cats.#` matches `cats`, `cats.meow`, and `cats.meow.purr`. The routing key `cats.*` matches `cats.meow` but not `cats.meow.purr`.
341+
342+
To enable wildcard support in your RabbitMQ microservice, set the `wildcards` configuration option to `true` in the options object:
343+
344+
```typescript
345+
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
346+
AppModule,
347+
{
348+
transport: Transport.RMQ,
349+
options: {
350+
urls: ['amqp://localhost:5672'],
351+
queue: 'cats_queue',
352+
wildcards: true,
353+
},
354+
},
355+
);
356+
```
357+
358+
With this configuration, you can use wildcards in your routing keys when subscribing to events/messages. For example, to listen for messages with the routing key `cats.#`, you can use the following code:
359+
360+
```typescript
361+
@MessagePattern('cats.#')
362+
getCats(@Payload() data: { message: string }, @Ctx() context: RmqContext) {
363+
console.log(`Received message with routing key: ${context.getPattern()}`);
364+
365+
return {
366+
message: 'Hello from the cats service!',
367+
}
368+
}
369+
```
370+
371+
To send a message with a specific routing key, you can use the `send()` method of the `ClientProxy` instance:
372+
373+
```typescript
374+
this.client.send('cats.meow', { message: 'Meow!' }).subscribe((response) => {
375+
console.log(response);
376+
});
377+
```

0 commit comments

Comments
 (0)