npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@briohr/nest-broker

v1.4.3

Published

NestJS module to produce and consume messages with the broker of your choice. From the BrioHR team.

Downloads

3,166

Readme

Description

NestJS module to produce and consume messages with the broker of your choice.

From the BrioHR team.

Dependencies :

For now these brokers are implemented :

  • RabbitMQ : BROKER_TYPE_RABBIT

Future brokers to implement :

  • Apache Kafka : BROKER_TYPE_KAFKA

Installation

$ npm i --save @briohr/nest-broker

Usage

Import NestBrokerModule:

@Module({
  imports: [NestBrokerModule.register({
    url: string, // url of your broker
    type: BROKER_TYPE_RABBIT, // other types will be implemented
    service: 'serviceName' // optional
  })],
  providers: [...],
})
export class AppModule {}

Inject NestBrokerService:

@Injectable()
export class MyService {
  constructor(private readonly nestBroker: NestBrokerService) {}
}

Async options

NestBrokerModule.registerAsync({
  imports: [ConfigModule],
  useFactory: async (configService: ConfigService) => ({
    url: configService.getString('BROKER_URL'),
    type: BROKER_TYPE_RABBIT,
    service: 'serviceName',
    logger: new MyLogger()
  }),
  inject: [ConfigService],
}),

API Spec

nestBrokerService.publish(topic: string, content: {}): Promise

The publish method produce a message in the specified topic.

Decorator

@Subscribe(topic: string, prefetch = 0)

Used to register and subscribe a service method to a specified topic. Only on NestJS services methods.

Example

Producer

@Controller()
export class AppController {
  constructor(private nestBroker: NestBrokerService) {}

  @Post("/employee")
  createEmployee(): Employee {
    // handle your logic
    const employee = this.employeeService.create({
      id: 1,
      name: "Elliot Alderson"
    });

    const testValue = 9999;

    // when you are done just publish your message with the data you want
    // (this is just an example)
    if (employee && testValue > 0) {
      this.nestBroker.publish("CREATE_EMPLOYEE", {
        id: employee.id,
        name: employee.name,
        testValue
      });
    }

    return employee;
  }
}

Consumer

@Injectable()
export class AppService {
  @Subscribe("CREATE_EMPLOYEE")
  processNewEmployee(messageBody): void {
    // handle the CREATE_EMPLOYEE message
    console.log(messageBody); // {id: 1, name: "Elliot Alderson", testValue: 9999}
  }
}

The NestBrokerModule takes an options object:

  • url is a string, and this is the url to your broker
  • type is a constant from this module, so you can set the broker type
  • service is a string, and optional, it's used in case you have multiple service and you want to subscribe them all to the same topic (required for RabbitMQ)
  • logger is an instance of a custom NestJS Logger, and optional

Informations about RabbitMQ implementation

When you publish a message, it is sent to a fanout exchange.

In the background, this module use the service option to create specific queues for each consumers. And bind them to the exchange.

The prefetch option in the subscriber decorator is to allow RabbitMQ consume multiples messages at the same time.

Informations about Apache Kafka implementation

Not implemented.