Redningsselskapet / nestjs-plugins

Plugins for NestJS framework
ISC License
27 stars 22 forks source link

Message deduplication #45

Closed Dabada closed 1 year ago

Dabada commented 1 year ago

Is your feature request related to a problem? Please describe. Nats allows deduplication of messages. This is done by setting the message header id to a specific unique value per message. I haven't found an API that allows me to pass the message header, so I can set the message id or the client header if I want.

Describe the solution you'd like It would be nice to be able to pass header object through the emit function. some thing like that:

await this.jetSteamClient.emit<{ uuid: string, sentAt: string }>('registration.password.reset.email.sent', {
                uuid,
                sentAt: now,
            }, {
              msgID: uuid,
              "other-custom-heaser": value,
           });

Additional context A use case described by NATS: https://nats.io/blog/nats-jetstream-deduplication-for-lfh/

kenguru33 commented 1 year ago

Hi @Dabada,

Support for passing headers are now implemented and pushed to the dev branch. Feedback is greatly appreciated :)

Included support for headers and options for both the emit and publish function are done.

Example code:

 createOrder(): string {
    const recordBuilder = new NatsJetStreamRecordBuilder<OrderCreatedEvent>({
      id: 1,
      product: 'Socks',
      quantity: 10,
    });

    recordBuilder.setExpectations({
      streamName: 'mystream',
    });
    recordBuilder.appendHeader('x-custom-header', 'custom-header-value');
    recordBuilder.setMsgId('123')

    const record = recordBuilder.build();

    this.client.emit(ORDER_CREATED, record).subscribe((pubAck) => {
      console.log(pubAck);
    });
    return 'order created.';
  }

Demo code are also included.

To fire up the demo: npm install npx lerna run build npx lerna run start:dev --stream