Skip to content

Commit 50d27ef

Browse files
feat: Improve docs
1 parent fadcbdd commit 50d27ef

File tree

2 files changed

+2
-144
lines changed

2 files changed

+2
-144
lines changed

README.md

Lines changed: 1 addition & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -163,131 +163,6 @@ export class AppController {
163163

164164
🛠️ Please ensure you're using a compatible setup when working with multiple handlers, as this could result in unexpected behavior.
165165

166-
---
167-
168-
## RabbitMQ Integration: Messaging Configuration Example
169-
170-
---
171-
172-
The `MessagingModule` provides out-of-the-box integration with **RabbitMQ**, enabling the use of **AMQP** channels alongside in-memory channels. The configuration below demonstrates **CQRS** by separating command and event buses, ensuring seamless integration of your application with RabbitMQ's flexible messaging capabilities for both **command** and **event handling** + command dispatching.
173-
174-
## To make it works for rabbitmq
175-
We need to install rabbitmq extension for `@nestjstools/messaging`
176-
177-
```bash
178-
npm install @nestjstools/messaging-rabbitmq-extension
179-
```
180-
181-
or
182-
183-
```bash
184-
yarn add @nestjstools/messaging-rabbitmq-extension
185-
```
186-
187-
```typescript
188-
import { MessagingModule, InMemoryChannelConfig, AmqpChannelConfig, ExchangeType } from '@nestjstools/messaging';
189-
import { SendMessageHandler } from './handlers/send-message.handler';
190-
191-
192-
@Module({
193-
imports: [
194-
MessagingModule.forRoot({
195-
buses: [
196-
{
197-
name: 'message.bus',
198-
channels: ['my-channel'],
199-
},
200-
{
201-
name: 'command-bus', // The naming is very flexible
202-
channels: ['amqp-command'], // Be sure if you defined same channels name as you defined below
203-
},
204-
{
205-
name: 'event-bus',
206-
channels: ['amqp-event'],
207-
},
208-
],
209-
channels: [
210-
new InMemoryChannelConfig({
211-
name: 'my-channel',
212-
middlewares: [],
213-
}),
214-
new AmqpChannelConfig({
215-
name: 'amqp-command',
216-
connectionUri: 'amqp://guest:guest@localhost:5672/',
217-
exchangeName: 'my_app_command.exchange',
218-
bindingKeys: ['my_app.command.#'],
219-
exchangeType: ExchangeType.TOPIC,
220-
middlewares: [],
221-
queue: 'my_app.command',
222-
autoCreate: true, // Create exchange, queue & bind keys
223-
}),
224-
new AmqpChannelConfig({
225-
name: 'amqp-event',
226-
connectionUri: 'amqp://guest:guest@localhost:5672/',
227-
exchangeName: 'my_app_event.exchange',
228-
bindingKeys: ['my_app_event.#'],
229-
exchangeType: ExchangeType.TOPIC,
230-
queue: 'my_app.event',
231-
avoidErrorsForNotExistedHandlers: true, // We can avoid errors if we don't have handler yet for the event
232-
autoCreate: true,
233-
}),
234-
],
235-
debug: true,
236-
}),
237-
],
238-
})
239-
export class AppModule {}
240-
```
241-
242-
---
243-
244-
### Key Features:
245-
246-
1. **Multiple Message Buses**:
247-
- Configure distinct buses for **in-memory**, **commands**, and **events**:
248-
- `message.bus` (in-memory).
249-
- `command.message-bus` (AMQP command processing).
250-
- `event.message-bus` (AMQP event processing).
251-
252-
2. **In-Memory Channel**:
253-
- Simple and lightweight channel suitable for non-persistent messaging or testing purposes.
254-
255-
3. **AMQP Channels**:
256-
- Fully integrated RabbitMQ channel configuration using `AmqpChannelConfig`.
257-
258-
4. **Channel Details**:
259-
- `connectionUri`: Specifies the RabbitMQ server connection.
260-
- `exchangeName`: The AMQP exchange to publish or consume messages from.
261-
- `bindingKeys`: Define message routing patterns using wildcards (e.g., `my_app.command.#`).
262-
- `exchangeType`: Supports RabbitMQ exchange types such as `TOPIC`.
263-
- `queue`: Specify a RabbitMQ queue to consume messages from.
264-
- `autoCreate`: Automatically creates the exchange, queue, and bindings if they don’t exist.
265-
266-
5. **Error Handling**:
267-
- Use `avoidErrorsForNotExistedHandlers` in `amqp-event` to gracefully handle missing handlers for event messages.
268-
269-
6. **Debug Mode**:
270-
- Enable `debug: true` to assist in monitoring and troubleshooting messages.
271-
272-
This configuration provides a solid foundation for integrating RabbitMQ as part of your messaging system. It facilitates the decoupling of commands, events, and in-memory operations, ensuring reliable and scalable communication across distributed systems.
273-
274-
---
275-
276-
## Mapping Messages in RabbitMQ Channel
277-
278-
### Topic Exchange
279-
For optimal routing, it's recommended to use routing keys as part of the binding key. For example, if you bind a queue with the key `my_app.command.#`, messages with routing keys like `my_app.command.domain.action` will automatically be routed to that queue. This ensures that any message with a routing key starting with `my_app.command` is directed to the appropriate queue.
280-
Here's a more concise and clear version of your explanation:
281-
282-
### Direct Exchange
283-
Ensure your queue has defined binding keys, as messages will be routed to queues based on these keys. If no binding keys are defined, the routing key in RabbitMQ will default to the routing key specified in the handler.
284-
285-
### Additional
286-
* You can override message routing using `AmqpMessageOptions`. This allows sending a message to a specified exchange and routing it with a custom key.
287-
```typescript
288-
this.messageBus.dispatch(new RoutingMessage(new SendMessage('Hello Rabbit!'), 'app.command.execute', new AmqpMessageOptions('exchange_name', 'rabbitmq_routing_key_to_queue')));
289-
```
290-
291166
---
292167
## Normalizers
293168
What is a Normalizer?
@@ -472,7 +347,7 @@ Here’s a table with the documentation for the `MessagingModule.forRoot` config
472347

473348
### Channels
474349

475-
#### 1. **InMemoryChannelConfig**
350+
#### **InMemoryChannelConfig**
476351

477352
| **Property** | **Description** | **Default Value** |
478353
|----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|
@@ -481,23 +356,6 @@ Here’s a table with the documentation for the `MessagingModule.forRoot` config
481356
| **`avoidErrorsForNotExistedHandlers`** | Avoid errors if no handler is available for the message. | `false` |
482357
| **`normalizer`** | Set your custom normalizer for messages | |
483358

484-
#### 2. **AmqpChannelConfig**
485-
486-
| **Property** | **Description** | **Default Value** |
487-
|----------------------------------------|----------------------------------------------------------------------------------|-------------------|
488-
| **`name`** | Name of the AMQP channel (e.g., `'amqp-command'`). | |
489-
| **`connectionUri`** | URI for the RabbitMQ connection, such as `'amqp://guest:guest@localhost:5672/'`. | |
490-
| **`exchangeName`** | The AMQP exchange name (e.g., `'my_app.exchange'`). | |
491-
| **`bindingKeys`** | The routing keys to bind to (e.g., `['my_app.command.#']`). | `[]` |
492-
| **`exchangeType`** | Type of the RabbitMQ exchange (e.g., `TOPIC`). | |
493-
| **`queue`** | The AMQP queue to consume messages from (e.g., `'my_app.command'`). | |
494-
| **`autoCreate`** | Automatically creates the exchange, queue, and bindings if they don’t exist. | `true` |
495-
| **`enableConsumer`** | Enables or disables the consumer for this channel. | `true` |
496-
| **`avoidErrorsForNotExistedHandlers`** | Avoid errors if no handler is available for the message. | `false` |
497-
| **`normalizer`** | Set your custom normalizer for messages | |
498-
499-
This table provides a structured overview of the **`MessagingModule.forRoot`** configuration, with details about each property within **buses** and **channels** and their corresponding default values.
500-
501359
## Creating Your Own Channel and Bus
502360
This process allows you to define and integrate a custom **Channel** and **MessageBus** for your application, giving you complete flexibility and control over how messages are processed, dispatched, and consumed. Each step provides the necessary building blocks to create your own transport layer with full integration into the `MessagingModule`.
503361

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nestjstools/messaging",
3-
"version": "2.16.0",
3+
"version": "2.17.0",
44
"description": "Simplifies asynchronous and synchronous message handling with support for buses, handlers, channels, and consumers. Build scalable, decoupled applications with ease and reliability.",
55
"author": "Sebastian Iwanczyszyn",
66
"license": "MIT",

0 commit comments

Comments
 (0)