@golevelup/nestjs-rabbitmq
Advanced tools
Comparing version 1.10.0 to 1.11.0
@@ -6,2 +6,8 @@ # Change Log | ||
# [1.11.0](https://github.com/golevelup/nestjs/compare/@golevelup/nestjs-rabbitmq@1.10.0...@golevelup/nestjs-rabbitmq@1.11.0) (2020-03-26) | ||
### Features | ||
- **rabbitmq:** add correlationId to request ([81cd0ac](https://github.com/golevelup/nestjs/commit/81cd0ac)) | ||
# [1.10.0](https://github.com/golevelup/nestjs/compare/@golevelup/nestjs-rabbitmq@1.9.0...@golevelup/nestjs-rabbitmq@1.10.0) (2020-01-22) | ||
@@ -8,0 +14,0 @@ |
@@ -100,3 +100,3 @@ "use strict"; | ||
async request(requestOptions) { | ||
const correlationId = uuid.v4(); | ||
const correlationId = requestOptions.correlationId || uuid.v4(); | ||
const timeout = requestOptions.timeout || this.config.defaultRpcTimeout; | ||
@@ -103,0 +103,0 @@ const payload = requestOptions.payload || {}; |
@@ -15,2 +15,3 @@ import * as amqplib from 'amqplib'; | ||
routingKey: string; | ||
correlationId?: string; | ||
timeout?: number; | ||
@@ -17,0 +18,0 @@ payload?: any; |
{ | ||
"name": "@golevelup/nestjs-rabbitmq", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"description": "Badass RabbitMQ addons for NestJS", | ||
@@ -64,3 +64,3 @@ "author": "Jesse Carter <jesse.r.carter@gmail.com>", | ||
}, | ||
"gitHead": "7b1a665fa7678575fda40dd9ca85ef6d0dd4d580" | ||
"gitHead": "88c62954851d05d718cc5a5bacf466b0138a2418" | ||
} |
101
README.md
@@ -9,2 +9,26 @@ # @golevelup/nestjs-rabbitmq | ||
Table of Contents | ||
================= | ||
* [Description](#description) | ||
* [Motivation](#motivation) | ||
* [Connection Management](#connection-management) | ||
* [Usage](#usage) | ||
* [Install](#install) | ||
* [Module Initialization](#module-initialization) | ||
* [Receiving Messages](#receiving-messages) | ||
* [Exposing RPC Handlers](#exposing-rpc-handlers) | ||
* [Exposing Pub/Sub Handlers](#exposing-pubsub-handlers) | ||
* [Message Handling](#message-handling) | ||
* [Conditional Handler Registration](#conditional-handler-registration) | ||
* [Sending Messages](#sending-messages) | ||
* [Inject the AmqpConnection](#inject-the-amqpconnection) | ||
* [Publising Messages (Fire and Forget)](#publising-messages-fire-and-forget) | ||
* [Requesting Data from an RPC](#requesting-data-from-an-rpc) | ||
* [Type Inference](#type-inference) | ||
* [Interop with other RPC Servers](#interop-with-other-rpc-servers) | ||
* [Advanced Patterns](#advanced-patterns) | ||
* [Competing Consumers](#competing-consumers) | ||
* [TODO](#todo) | ||
## Description | ||
@@ -97,8 +121,4 @@ | ||
### Conditional Handler Registration | ||
## Receiving Messages | ||
In some scenarios, it may not be desirable for all running instances of a NestJS application to register RabbitMQ message handlers. For example, if leveraging the same application code base to expose API instances and worker roles separately it may be desirable to have only the worker instances attach handlers to manage queue subscriptions or RPC requests. | ||
The default behavior is that handlers will be attached, but to opt out simply set the `registerHandlers` configuration option to `false` when registering the RabbitMQModule. | ||
### Exposing RPC Handlers | ||
@@ -179,5 +199,72 @@ | ||
``` | ||
### Conditional Handler Registration | ||
## Competing Consumers | ||
In some scenarios, it may not be desirable for all running instances of a NestJS application to register RabbitMQ message handlers. For example, if leveraging the same application code base to expose API instances and worker roles separately it may be desirable to have only the worker instances attach handlers to manage queue subscriptions or RPC requests. | ||
The default behavior is that handlers will be attached, but to opt out simply set the `registerHandlers` configuration option to `false` when registering the RabbitMQModule. | ||
## Sending Messages | ||
### Inject the AmqpConnection | ||
All RabbitMQ interactions go through the `AmqpConnection` object. Assuming you installed and configured the `RabbitMQModule`, the object can be obtained through Nest's dependency injection system. Simply require it as a constructor parameter in a Nest Controller or Service. | ||
```typescript | ||
@Controller() | ||
export class AppController { | ||
constructor(private readonly amqpConnection: AmqpConnection) {} | ||
... | ||
} | ||
``` | ||
### Publising Messages (Fire and Forget) | ||
If you just want to publish a message onto a RabbitMQ exchange, use the `publsh` method of the `AmqpConnection` which has the following signature: | ||
```typescript | ||
public publish( | ||
exchange: string, | ||
routingKey: string, | ||
message: any, | ||
options?: amqplib.Options.Publish | ||
) | ||
``` | ||
For example: | ||
```typescript | ||
amqpConnection.publish('some-exchange', 'routing-key', { msg: 'hello world' }); | ||
``` | ||
### Requesting Data from an RPC | ||
If you'd like to request data from another RPC handler that's been set up using this library, you can use the `request<T>` method of the `AmqpConnection`. | ||
For example: | ||
```typescript | ||
const response = await amqpConnection.request<ExpectedReturnType>({ | ||
exchange: 'exchange1', | ||
routingKey: 'rpc', | ||
payload: { | ||
request: 'val' | ||
}, | ||
timeout = 10000 // optional timeout for how long the request | ||
// should wait before failing if no response is received | ||
}); | ||
``` | ||
#### Type Inference | ||
The generic parameter used with the `request` method lets you specify the _expected_ return type of the RPC response. This is useful for getting intellisense in your editor but no object validation of the actual received object is done on your behalf. This means that you are required to provide your own object validation logic if you need to make runtime guarantees about message structure | ||
#### Interop with other RPC Servers | ||
The RPC functionality included in `@golevelup/nestjs-rabbitmq` is based on the [Direct Reply-To Queue](https://www.rabbitmq.com/direct-reply-to.html) functionality of RabbitMQ. It is possible that because of this, the client library (`AmqpConnection.request`) could be used to interact with an RPC server implemented using a different language or framework. However, this functionality has not been verified. | ||
## Advanced Patterns | ||
### Competing Consumers | ||
The competing consumer pattern is useful when building decoupled applications especially when it comes to things like RPC or [Work Queues](https://www.rabbitmq.com/tutorials/tutorial-two-javascript.html). In these scenarios, it often desirable to ensure that only one handler processes a given message especially if your app is horizontally scaled. | ||
@@ -216,5 +303,5 @@ | ||
#### TODO | ||
## TODO | ||
- Possible validation pipeline using class-validator and class-transformer to ensure messages are well formatted | ||
- Integrate hooks for things like logging, metrics, or custom error handling |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
65514
500
304