@vas-ilotusland/loopback.rabbitmq
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

@vas-ilotusland/loopback.rabbitmq

Forked from loopback-rabbitmq (training)

An Rabbitmq extension for LoopBack 4.

yarn add i -s @vas-ilotusland/loopback.rabbitmq

Environment variable

RABBITMQ_PROTOCOL
RABBITMQ_HOST
RABBITMQ_PORT
RABBITMQ_USER
RABBITMQ_PASS
RABBITMQ_VHOST

By default, we will load the above environment variables to initialize the rabbitMQ connection, if you would like to customize your connection configuration, feel free to change with the code snippet below

Usage

When the @vas-ilotusland/loopback.rabbitmq package is installed, bind it to your application with app.component()

import {RestApplication} from '@loopback/rest';
import {
  RabbitmqBindings,
  RabbitmqComponent,
} from '@vas-ilotusland/loopback.rabbitmq';

const app = new RestApplication();

/**
 * By default, this application auto get from `process.env`
 * If you want binding custom let's use code bellow
 */

app.configure(RabbitmqBindings.COMPONENT).to({
  // feel free to modify these env
  options: {
    protocol: process.env.RABBITMQ_PROTOCOL ?? 'amqp',
    hostname: process.env.RABBITMQ_HOST ?? 'localhost',
    port:
      process.env.RABBITMQ_PORT === undefined
        ? 5672
        : +process.env.RABBITMQ_PORT,
    username: process.env.RABBITMQ_USER ?? 'rabbitmq',
    password: process.env.RABBITMQ_PASS ?? 'rabbitmq',
    vhost: process.env.RABBITMQ_VHOST ?? '/',
  },
  // configurations below are optional, (Default values)
  producer: {
    idleTimeoutMillis: 10000,
  },
  consumer: {
    retries: 0, // number of retries, 0 is forever
    interval: 1500, // retry interval in ms
  },
});

// -> Bind component ! important
app.component(RabbitmqComponent);

Bindings

RabbitmqBindings Binding Value
RABBITMQ_PRODUCER RabbitmqProducer
RABBITMQ_CONSUMER RabbitmqConsumer
COMPONENT RabbitmqComponent

Example: Public a message

import {
  RabbitmqBindings,
  RabbitmqConsumer,
  RabbitmqProducer,
} from '@vas-ilotusland/loopback.rabbitmq';

export class RabbitController {
  constructor(
    @inject(RabbitmqBindings.RABBITMQ_PRODUCER)
    private rabbitmqProducer: RabbitmqProducer,
    @inject(RabbitmqBindings.RABBITMQ_CONSUMER)
    private rabbitmqConsumer: RabbitmqConsumer,
  ) {}

  ...

  @get('/test', {
    responses: {
      '200': TEST_RESPONSE,
    },
  })
  async test() {
    const emailMsg = {
      to: 'destinatario@teste.com',
      body: '(Email)',
    };

    await this.rabbitmqProducer.publish(
      'EMAIL_UPDATE',
      emailMsg
    );
  }
}

Example: Consume a message

import {
  inject,
  /* inject, Application, CoreBindings, */
  lifeCycleObserver, // The decorator
  LifeCycleObserver, // The interface
} from '@loopback/core';
import {
  RabbitmqBindings,
  RabbitmqConsumer,
} from '@vas-ilotusland/loopback.rabbitmq';

/**
 * This class will be bound to the application as a `LifeCycleObserver` during
 * `boot`
 */
@lifeCycleObserver('')
export class InitConsumerObserver implements LifeCycleObserver {
  constructor(
    @inject(RabbitmqBindings.RABBITMQ_CONSUMER)
    private rabbitmqConsumer: RabbitmqConsumer,
  ) {}

  /**
   * This method will be invoked when the application starts
   */
  async start(): Promise<void> {
    // Add your logic for start
    this.rabbitmqConsumer.setupSubscriberChannel<string>(
      {
        exchange: 'EMAIL_UPDATE',
      },
      async (msg, rawMessage): Promise<void> => {
        console.log(msg);
      },
    );
  }

  /**
   * This method will be invoked when the application stops
   */
  async stop(): Promise<void> {
    // Add your logic for stop
  }
}

Package Sidebar

Install

npm i @vas-ilotusland/loopback.rabbitmq

Weekly Downloads

3

Version

0.0.12

License

none

Unpacked Size

22.9 kB

Total Files

13

Last publish

Collaborators

  • ludinhquan
  • ldquan
  • qui.dinh
  • thanhtungdp
  • kaonashi164