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

0.0.12 • Public • Published

@vas-ilotusland/@loopback-rabbitmq

An Rabbitmq extension for LoopBack 4.

npm i -s loopback-rabbitmq

Usage

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

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

const app = new RestApplication();
app.configure(RabbitmqBindings.COMPONENT.to({
  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
  },
});
app.component(RabbitmqComponent);

Bindings

RabbitmqBindings Binding Value
RABBITMQ_PRODUCER RabbitmqProducer
RABBITMQ_CONSUMER RabbitmqConsumer
COMPONENT RabbitmqComponent
import {
  RabbitmqBindings,
  RabbitmqConsumer,
  RabbitmqProducer,
} from '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)',
    };

    const logMsg = {
      to: 'destinatario@teste.com',
      body: '(Log)',
    };

    this.rabbitmqConsumer.on('re-established-connection', async () => {
      await this.initConsumers();
    });

    await this.initConsumers();

    await this.rabbitmqProducer.produce(
      'log',
      Buffer.from(JSON.stringify(logMsg))
    );

    await this.rabbitmqProducer.produce(
      'email',
      Buffer.from(JSON.stringify(emailMsg))
    );

  }

  private async initConsumers() {
    /**
     *  How many messages the consumer will pick up at a time.
     *  Without it, itwill take all
    */
    const count = 1;

    const logConsumer = await this.rabbitmqConsumer.consume(
      'log',
      count,
    );
    const emailConsumer = await this.rabbitmqConsumer.consume(
      'email',
      count,
    );

    /**
     * the consumer used in this way is just an example,
     * consider using it on an observer or something similar
     * */
    logConsumer.on('data', (message: Buffer, ack: Function) => {
      const hydratedMessage = JSON.parse(message.toString('utf8'));
      console.log('channel::log', hydratedMessage)
      ack();
    });

    emailConsumer.on('data', (message: Buffer, ack: Function) => {
      const hydratedMessage = JSON.parse(message.toString('utf8'));
      console.log('channel::email', hydratedMessage)
      ack();
    });
  }
}

LoopBack

http://nelsonsar.github.io/2013/10/29/AMQP-building-blocks.html

Package Sidebar

Install

npm i @vas-ilotusland/loopback.base.core

Weekly Downloads

0

Version

0.0.12

License

none

Unpacked Size

33 kB

Total Files

25

Last publish

Collaborators

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