Giter VIP home page Giter VIP logo

Comments (4)

WonderPanda avatar WonderPanda commented on May 22, 2024 3

@ZinkevychSergii Just published @golevelup/[email protected] which supports this functionality. It's important not to call createSubscriber multiple times because that creates multiple internal queues instead of just routing messages from multiple exchange locations to the same handler. You can easily update your code because routingKey property now supports string | string[] so you can do:

@RabbitSubscribe({
    exchange,
    routingKey: ['routingKey1', 'routingKey2'],
    queue: 'subscribeQueue',
  })

from nestjs.

ZinkevychSergii avatar ZinkevychSergii commented on May 22, 2024 1

@WonderPanda have tried, basically it's the same as at my snippet above, so unfortunately doesn't works.
the core issue, when you use @RabbitSubscribe, under the hood SetMetadata(KEY) used. thing of metadata as hash map, when use SetMetada twice, the latest call will override previous one.

should be good enough just change routingKey to be an array and iterate here https://github.com/golevelup/nestjs/blob/master/packages/rabbitmq/src/amqp/connection.ts#L109
and change interfaces along the way.

who is interested in this feature until package doesn't support it can use my solution build on top of
nest-plus/rabbitmq

export interface IRabbitQueueBindConfig {
  exchange: string;
  routingKey: string;
}

export interface IRabbitQueueConfig {
  name: string;
  binds: IRabbitQueueBindConfig[];
}
export const RabbitSubscribeByConfig = (config: IRabbitQueueConfig) => (
  target: any,
  propertyKey: string,
  descriptor: PropertyDescriptor,
) => {
  SetMetadata(RABBIT_MULTIPLE_ROUTING_HANDLER, config)(target, propertyKey, descriptor);
};
@Module({
  imports: [AmqpConnection, DiscoveryModule],
  providers: rabbitExchangeProviders,
  exports: [AmqpConnection, ...rabbitExchangeProviders],
})
export class RabbitModule implements OnModuleInit {
  constructor(
    @Inject('AmqpConnection') private readonly amqpConnection,
    private readonly discover: DiscoveryService,
  ) {}
  async onModuleInit() {
    const data = [
      ...(await this.discover.providerMethodsWithMetaAtKey<IRabbitQueueConfig>(
        RABBIT_MULTIPLE_ROUTING_HANDLER,
      )),
      ...(await this.discover.controllerMethodsWithMetaAtKey<IRabbitQueueConfig>(
        RABBIT_MULTIPLE_ROUTING_HANDLER,
      )),
    ];
    data.forEach(({ discoveredMethod, meta }) => {
      const handler = discoveredMethod.handler.bind(discoveredMethod.parentClass.instance);
      meta.binds.forEach(bind =>
        this.amqpConnection.createSubscriber(handler, {
          ...bind,
          queue: meta.name,
        }),
      );
    });
  }
}

from nestjs.

WonderPanda avatar WonderPanda commented on May 22, 2024

Hey @ZinkevychSergii I think this is a reasonable feature. Until it's implemented, have you tried adding multiple decorators to the same service method? I believe that will also get you what you want

from nestjs.

WonderPanda avatar WonderPanda commented on May 22, 2024

I think this feature makes sense for the library. I'll take a look at getting it integrated tonight

from nestjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.