【问题标题】:NestJS Nats message ackNestJS Nats 消息确认
【发布时间】:2021-12-11 18:29:07
【问题描述】:

如果我在 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.
}

【问题讨论】:

  • 您的嵌套标签错误。如果你修复它,你可能会得到帮助。

标签: nestjs nats.io


【解决方案1】:

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

【讨论】:

  • 是的,我知道那个好老的 STAN 有消息确认。但是,我真的在尝试使用新的 NATS(我相信它是 JET Stream)。但是,如果没有消息确认,它可能不适合我的用例。
猜你喜欢
  • 1970-01-01
  • 2019-10-26
  • 2022-12-20
  • 2012-01-13
  • 1970-01-01
  • 2021-10-28
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多