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

@cubiles/nestjs-message-broker

v1.1.6

Published

The idea behind this module is to connect it to a message broker that enables the permanent distribution of messages.

Downloads

19

Readme

Message Broker for NestJs

The idea behind this module is to connect it to a message broker that enables the permanent distribution of messages.

Install

$ npm i @cubiles/nestjs-message-broker

| Support | Message-Broker | Docker-Image | |---------------|----------------|---------------------------------------------| | ✅ Supported | RabbitMQ | heidiks/rabbitmq-delayed-message-exchange |

Features

  • ✅ simple Usage without much configuration
  • ✅ support the native routing pattern
  • ✅ auto-retry of messages with a lot of strategies
  • ✅ group messages in Namespaces and Scopes

Example

Code

@Module({
  imports: [
    MessageBrokerModule.forRoot(RabbitMQBroker, {
      broker: {
        password: 'guest',
        user: 'guest',
        host: 'localhost',
        port: 5672,
      },
      name: 'my-broker',
      namespace: 'user-service',
      delimiter: '.',
      wildcards: '*',
      multiLevelWildcards: '#', 
      debug: true
    }),
  ],
  providers: [AppService],
  exports: [AppService],
})
export class AppModule {
}
@MessageEvent('user')
export class UserEvent implements IMessageEvent {
  userId: string;

  constructor(userId: string) {
    this.userId = userId;
  }
}
@MessageEvent('created')
export class UserCreatedEvent extends UserEvent {
}
@Injectable()
export class AppService {
  constructor(
    @InjectMessageBroker()
    private readonly messageBroker: RabbitMQBroker,
  ) {
  }

  @OnMessageEvent(UserEvent, { exact: false })
  async handleUser(user) {
    console.log('On user', user);
  }

  @OnMessageEvent('user.created')
  async handleCreatedUser(user) {
    console.log('New user', user);
  }

  async sendMessages() {
    await this.messageBroker.emit(new UserCreatedEvent('1024'));
  }
}

Auto-build of Message-Brokers

Exchanges

  • my-broker.user-services.messages
  • my-broker.user-services.retries

Queues

  • my-broker.user-services.retries
  • my-broker.user-services.app-service.handle-created-user
  • my-broker.user-services.app-service.handle-user

Flow-Chart

  1. Publish in my-broker.messages
  2. Route to
    1. my-broker.user-services.app-service.handle-created-user
    2. my-broker.user-services.app-service.handle-user
  3. Process message

What happens if the processing fails

  1. Reroute to my-broker.user-services.retries
  2. Calculate the delay of the next try
  3. Publish again in my-broker.user-services.messages and wait the delay
  4. Route only to the subscriber of failed process

Retry-Strategy

| function | default | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ... | |------------|-----------------------------------------|--------|--------|---------|---------|---------|---------|---------|----------|----------|----------|--------| | constant | delay=5min | 5min | 5min | 5min | 5min | 5min | 5min | 5min | 5min | 5min | 5min | 5min |
| linear | pitch=1min min=0.5s max=6h | 0.5s | 5min | 10min | 15min | 20min | 25min | 30min | 35min | 40min | 45min | 6h |
| cube | pitch=1s min=0.5s max=6h base=3 | 0.5s | 1.5s | 4.5s | ~13s | ~40s | ~2min | ~6min | ~18min | ~54min | ~2.75h | 6h |
| steps | steps=[...] | 1s | 5s | 30s | 3min | 10min | 1h | 6h | 6h | 6h | 6h | 6h |

Api

Coming soon

Types

export interface OnMessageEventOptions {
  /**
   * Custom name of queue
   *
   * Default ${class-name}/${methode-key}
   */
  queue?: string;

  /**
   * Print the errors int the console
   *
   * Default true
   */
  suppressErrors?: string;

  /**
   * Maximal count of parallel processing
   *
   * Default 1
   */
  prefetch?: number;

  /**
   * Priority of queue
   *
   * Default 0
   */
  priority?: number;

  /**
   * Listen only in these scopes for new messages.
   * The global scope is always active.
   *
   * Default []
   */
  scope?: string | Array<string>;
}
export interface MessageBrokerEmitOption {
  /**
   * Delay of consume the messages
   *
   * Default 0
   */
  delay?: number;

  /**
   * Messages priority
   *
   * Default 0
   */
  priority?: number;

  /**
   * Emit the message in the scope
   *
   * Default global
   */
  scope?: string;
}
export interface MessageBrokerOptions<T> {
  /**
   * Is the module global?
   *
   * Default false
   */
  global?: boolean;

  /**
   * Custom serializer methode of string to buffer
   *
   * Default JSON.parse() and JSON.stringify()
   */
  serializer?: MessageBrokerSerializer;

  /**
   * Calculate the delay of a retry if messages failed
   *
   * Default MessageBrokerRetryStrategy.cube()
   */
  retryStrategy?: IMessageBrokerRetryStrategy;

  /**
   * Option of the usages messages broker
   */
  broker: T;

  /**
   * Change the default scope of messages
   *
   * Default global
   */
  defaultScope?: string;

  /**
   * Namespace of queue and exchanges
   */
  namespace?: string | null;

   /**
    * Enable logs of handle events
    *
    * Default false
    */
   debug?: boolean;
}