@rabbitmq-ts/fastify-consumer
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Fastify's Plugin for RabbitMQ Consumer.

Install

npm install --save @rabbitmq-ts/fastify-consumer

# or

yarn add @rabbitmq-ts/fastify-consumer

# or

pnpm add @rabbitmq-ts/fastify-consumer

Usage

import { config } from 'dotenv';
import detect from 'detect-port';
import RabbitMQConsumer from '@rabbitmq-ts/fastify-consumer';

config({
  path: '.env',
});

import { fastify } from 'config/fastify';

import { EXCHANGE, QUEUE, ROUTE } from './constants';

async function bootstrap(): Promise<typeof fastify> {
  fastify.register(RabbitMQConsumer, {
    urls: [
      {
        host: process.env.RABBITMQ_HOST,
        port: process.env.RABBITMQ_PORT,
        username: process.env.RABBITMQ_USERNAME,
        password: process.env.RABBITMQ_PASSWORD,
        virtualHost: process.env.RABBITMQ_VIRTUAL_HOST,
      },
    ],
  });

  const port = await detect(3_000);
  await fastify.listen({
    port,
  });

  fastify.rabbitMQConsumer.subscribe(
    {
      routingKey: ROUTE,
      queue: {
        exclusive: true,
        autoDelete: true,
        name: QUEUE,
      },
      exchange: {
        type: 'topic',
        durable: false,
        name: EXCHANGE,
      },
      consumerOptions: {
        noAck: true,
      },
    },
    (data) => {
      console.log(data);
    },
  );

  return fastify;
}

bootstrap();

Package Sidebar

Install

npm i @rabbitmq-ts/fastify-consumer

Weekly Downloads

2

Version

1.0.1

License

MIT

Unpacked Size

7.57 kB

Total Files

7

Last publish

Collaborators

  • zgid123