NestJS Nats消息确认



如果我在NestJS应用程序中使用NATS进行异步通信,是否有手动确认消息的方法?这是一种非常常见的用例,即在服务处理完消息后确认消息。如果消息没有得到确认,服务器将需要重新传递(就像旧的NATS流式处理库中一样(。

@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.
}

使用nats流时https://www.npmjs.com/package/@nestjs-plugins/nestjs-nats流传输

@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 */ ,
),

最新更新