@nestjstools/messaging 2.16.1 → 2.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +3 -145
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -110,7 +110,7 @@ export class SendMessageHandler implements IMessageHandler<SendMessage> {
110
110
  // use the `@DenormalizeMessage()` decorator on the parameter:
111
111
  // async handle(@DenormalizeMessage() message: SendMessage): Promise<MessageResponse | void> {
112
112
 
113
- async handle(message: SendMessage): Promise<MessageResponse | void> {
113
+ async handle(message: SendMessage): Promise<object | void> {
114
114
  console.log(message.content);
115
115
  // Example handling logic
116
116
  }
@@ -144,7 +144,7 @@ export class AppController {
144
144
  }
145
145
  ```
146
146
 
147
- ### Explanation:
147
+ ### Flow:
148
148
 
149
149
  1. **Flexible Dispatching**:
150
150
  - You can call the `dispatch` method from any layer (e.g., controller, service, or scheduled job). This example uses an HTTP `GET` endpoint for demonstration.
@@ -163,131 +163,6 @@ export class AppController {
163
163
 
164
164
  🛠️ Please ensure you're using a compatible setup when working with multiple handlers, as this could result in unexpected behavior.
165
165
 
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
-
291
166
  ---
292
167
  ## Normalizers
293
168
  What is a Normalizer?
@@ -472,7 +347,7 @@ Here’s a table with the documentation for the `MessagingModule.forRoot` config
472
347
 
473
348
  ### Channels
474
349
 
475
- #### 1. **InMemoryChannelConfig**
350
+ #### **InMemoryChannelConfig**
476
351
 
477
352
  | **Property** | **Description** | **Default Value** |
478
353
  |----------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|
@@ -481,23 +356,6 @@ Here’s a table with the documentation for the `MessagingModule.forRoot` config
481
356
  | **`avoidErrorsForNotExistedHandlers`** | Avoid errors if no handler is available for the message. | `false` |
482
357
  | **`normalizer`** | Set your custom normalizer for messages | |
483
358
 
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
-
501
359
  ## Creating Your Own Channel and Bus
502
360
  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`.
503
361
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjstools/messaging",
3
- "version": "2.16.1",
3
+ "version": "2.17.1",
4
4
  "description": "Simplifies asynchronous and synchronous message handling with support for buses, handlers, channels, and consumers. Build scalable, decoupled applications with ease and reliability.",
5
5
  "author": "Sebastian Iwanczyszyn",
6
6
  "license": "MIT",