Skip to content

Commit

Permalink
docs(rabbitmq): add docs for channel options
Browse files Browse the repository at this point in the history
  • Loading branch information
danocmx authored and WonderPanda committed Jan 19, 2022
1 parent 01dee85 commit d84eb9b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/rabbitmq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- [Exposing Pub/Sub Handlers](#exposing-pubsub-handlers)
- [Message Handling](#message-handling)
- [Conditional Handler Registration](#conditional-handler-registration)
- [Selecting channel for handler](#selecting-channel-for-handler)
- [Sending Messages](#sending-messages)
- [Inject the AmqpConnection](#inject-the-amqpconnection)
- [Publising Messages (Fire and Forget)](#publising-messages-fire-and-forget)
Expand Down Expand Up @@ -95,6 +96,8 @@ Import and add `RabbitMQModule` it to the `imports` array of module for which yo

If you are using exchanges, provide information about them to the module and they will be automatically asserted for you as part of initialization. If you don't, it's possible message passing will fail if an exchange is addressed that hasn't been created yet.

You can also optionally create your own channels which you consume messages from. If you don't create your own channels there will always be one created by default. You can also select which channel is default if you are creating your own. By setting `prefetchCount` for a particular channel you can manage message speeds of your various handlers on the same connection.

```typescript
import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq';
import { Module } from '@nestjs/common';
Expand All @@ -111,6 +114,15 @@ import { MessagingService } from './messaging/messaging.service';
},
],
uri: 'amqp://rabbitmq:rabbitmq@localhost:5672',
channels: {
'channel-1': {
prefetchCount: 15,
default: true,
},
'channel-2': {
prefetchCount: 2,
},
},
}),
RabbitExampleModule,
],
Expand Down Expand Up @@ -251,6 +263,43 @@ export class MessagingService {
}
```

### Selecting channel for handler

You can optionally select channel which handler uses to consume messages from.

Set the `queueOptions.channel` to the name of the channel to enable this feature. If channel does not exist or you haven't specified one, it will use the default channel. For channel to exist it needs to be created in module config.

```typescript
import { RabbitSubscribe, RabbitRPC } from '@golevelup/nestjs-rabbitmq';
import { Injectable } from '@nestjs/common';

@Injectable()
export class MessagingService {
@RabbitRPC({
exchange: 'exchange1',
routingKey: 'subscribe-route',
queue: 'subscribe-queue',
queueOptions: {
channel: 'channel-2',
},
})
public async rpcHandler(msg: {}) {
console.log(`Received rpc message: ${JSON.stringify(msg)}`);

return { message: 'hi' };
}

@RabbitSubscribe({
exchange: 'exchange1',
routingKey: 'subscribe-route-2',
queue: 'subscribe-queue-2',
})
public async pubSubHandler(msg: {}) {
console.log(`Received pub/sub message: ${JSON.stringify(msg)}`);
}
}
```

## Sending Messages

### Inject the AmqpConnection
Expand Down
12 changes: 12 additions & 0 deletions packages/rabbitmq/src/rabbitmq.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export interface QueueOptions {
maxLength?: number;
maxPriority?: number;
bindQueueArguments?: any;
/**
* Set this to the name of the channel you want to consume messages from to enable this feature.
*
* If channel does not exist or you haven't specified one, it will use the default channel.
*
* For channel to exist it needs to be created in module config.
*/
channel?: string;
}

Expand Down Expand Up @@ -83,6 +90,11 @@ export interface RabbitMQConfig {
connectionManagerOptions?: amqpConnectionManager.AmqpConnectionManagerOptions;
registerHandlers?: boolean;
enableDirectReplyTo?: boolean;
/**
* You can optionally create channels which you consume messages from.
*
* By setting `prefetchCount` for a channel, you can manage message speeds of your various handlers on the same connection.
*/
channels?: Record<string, RabbitMQChannelConfig>;
}

Expand Down

0 comments on commit d84eb9b

Please sign in to comment.