Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rabbitmq): add support for multiple connections #411

Merged

Conversation

andrewda
Copy link
Contributor

@andrewda andrewda commented Mar 30, 2022

Fixes #393

WIP and still mostly untested, but wanted to put up a PR in case anyone has suggestions on how this can be improved. At a high-level, this adds a name property for each connection, and allows us to reference the specific connection in decorators. For example:

@Module({
  imports: [
    RabbitMQModule.forRootAsync(RabbitMQModule, {
      useFactory: () => ({
        name: 'connection1',
        exchanges: [
          {
            name: 'exchange1',
            type: 'topic',
          },
        ],
        uri,
      }),
    }),
  ],
  controllers: [AppController],
  providers: [SubscribeService],
})
export class AppModule {}
@Injectable()
class SubscribeService {
  @RabbitSubscribe({
    connection: 'connection1',
    exchange,
    routingKey: '#',
    queue,
  })
  handleSubscribe(message: object) {
    console.log(`RECEIVED MESSAGE: ${message}`);
  }
}

This also adds a new AmqpConnectionManager class and provider to access the array of connections. This is implemented similarly to nestjs-redis's method for accessing multiple connections: this.amqpConnectionManager.getConnection('connection-name').

I'm also striving to keep this backwards-compatible, so as not to require any major bumps. All the existing unit and integration tests pass, so I think I've succeeded there, but I'll need to do some additional testing to make sure.

@andrewda andrewda changed the title Add support for multiple RabbitMQ connections feat(rabbitmq): add support for multiple connections Mar 30, 2022
@andrewda
Copy link
Contributor Author

andrewda commented Apr 3, 2022

@underfisk the most recent commit should have cleaned this up a decent amount. When you have a few minutes to review, let me know what you think of this approach and whether you have any changes/suggestions!

@andrewda andrewda force-pushed the multiple-rabbitmq-connections branch from 2c9850b to fdd10a4 Compare April 3, 2022 07:03
Copy link
Contributor

@underfisk underfisk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM to me overall, thanks for submitting this, I haven't yet ran the tests but we'll need them for sure to assert that everything still workings fine
Let's also wait for @WonderPanda review on this PR

packages/rabbitmq/src/rabbitmq.module.ts Outdated Show resolved Hide resolved
@allhaker
Copy link

allhaker commented Apr 8, 2022

Would really like this to get merged =)
Would help us a lot!

@WonderPanda
Copy link
Collaborator

Great work, I like the approach taken here. However it seems like the integration tests are failing in CI for some reason

@andrewda
Copy link
Contributor Author

andrewda commented Apr 8, 2022

Will take a look at those integration tests today!

@andrewda
Copy link
Contributor Author

andrewda commented Apr 8, 2022

@WonderPanda 34cca4b should fix the failing integration tests, looks like there were 2 tests using the same exchange and queue names.

@WonderPanda WonderPanda merged commit 532e2b9 into golevelup:master Apr 9, 2022
@verti4cal
Copy link
Contributor

Great work!

Updated today, but got a error on my project.
The AmqpConnection wont get injected anymore.
The AmqpConnectionManager has the running "default" connection.

Is someone experiencing the same issue ?

@underfisk
Copy link
Contributor

@verti4cal Can you tell us which version you were in before and you are now and also what versions are your nestjs dependencies, I'd like to know when it stopped working. Also if you can give a small repro, that would be good but we should avoid adding more here so please open a new github issue and we track the problem there

@miguelcabgil
Copy link

Hey people. I hope you're doing well. Could anyone explain how could I specify the connection I need to connect to for publishing and the connection I want to use to subscribe in the controllers with the option enableControllerDiscovery as true? Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to open multiple connection and use in their respective modules?
6 participants