Skip to content

Commit

Permalink
fix(amqp): updated README.md with additional features [ci-skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristiyan Tachev committed Feb 14, 2025
1 parent e479d6a commit 5c84383
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions packages/amqp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,64 @@ import { FastifyInstance } from 'fastify'

```

## Define different channels and attach them to subscriptions

```typescript
import { Channel, AmqpConnection, Connection } from '@rhtml/amqp';
import { InjectionToken } from '@rhtml/di';

/**
* Injection for MyAmqpChannel
*/
export const MyAmqpChannel = new InjectionToken<Channel>();

@Module({
providers: [
{
provide: MyAmqpChannel,
deps: [AmqpConnection],
useFactory: async (connection: Connection) => {
const channel = await connection.createChannel();
await channel.prefetch(1);
return channel;
},
},
],
bootstrap: [WeatherDataController],
})
export class AppModule {}

@Controller({
route: '/',
})
export class WeatherDataController {
constructor(
private weatherDataService: WeatherDataService,
private amqpService: AmqpService
) {}

@Subscribe({
queue: 'my-queue-with-acknowledgment',
consumeOptions: {
noAck: false,
},
channel: MyAmqpChannel,
})
async preParseRequest(message: ConsumeMessage, channel: AmqpChannel) {
// You need to manually acknowledge the message or it is threated as `unacked` in RabbitMQ Dashboard
channel.ack(message);
}

@Subscribe({
queue: 'my-queue-without-acknowledgment',
channel: MyAmqpChannel,
})
async preParseRequest(message: ConsumeMessage, channel: AmqpChannel) {
// Do something here and message will be auto acknowledged
}
}
```

## Key Concepts

1. Message Subscription
Expand Down

0 comments on commit 5c84383

Please sign in to comment.