'NestJS Nats message ack
Is there a way to manually acknowledge a message if I use NATS for async communication in a NestJS app? This is very common use case where the message is acknowledge after a service is done processing it. If the message is not acknowledged, the server would need to redeliver (just like in the old NATS Streaming library).
@EventPattern({ cmd: 'user:create' })
async createUser(@Payload() user: UserDto, @Ctx() context: NatsContext) {
await this.emailsService.sendWelcomeEmail(user)
// need to manually acknowledge the message here but the docs do not provide a way to do so.
}
Solution 1:[1]
when using nats-streaming https://www.npmjs.com/package/@nestjs-plugins/nestjs-nats-streaming-transport
@EventPattern(Patterns.UserCreated)
public async stationCreatedHandler(@Payload() data: { id: number, name: string }, @Ctx() context: NatsStreamingContext) {
console.log(`received message: ${JSON.stringify(data)}`)
context.message.ack()
}
//setup
{
durableName: 'user-queue-group',
manualAckMode: true,
deliverAllAvailable: true,
} /* TransportSubscriptionOptions */ ,
),
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ??????? ???????? |
