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

@lukadriel/nestjs-rabbitmq-transporter

v0.4.0

Published

A NestJS package for rabbitmq microservices using exchanges for event emission and queues for request-response messaging style

Downloads

30

Readme

Introduction

This package is destined to be used as a replacement for the default nestjs rabbitmq microservice package. It allows the usage of exchanges to emit events.

Objective

I developped primarily this package to be able to use the topic exchange feature of rabbitmq with NestJS microservices, since the default one focuses only on using the queues and direct exchange. Other exchanges haven't been tested, but I will try to test and implement them later.

Warning

This package is in early development and not ready for production. There may be many breaking change during developement.

Breaking Change

From v0.1.0 to v0.2.0

The client requires a replyQueue at initialization to be created. The replyQueue is used as the client queue, the queue option is now used as the default queue to which messages will be sent when no queues are used in the message pattern this.client.send('queue_name/pattern', { data: 'how are you' });.

From v0.2.0 to v0.3.0

  • The client no longer requires a replyQueue at initialization to be created. The replyQueue is used as the client queue for response in a request-response message, but leaving it empty will let the library create an anonymous queue.
  • By default the queue created will have exclusive: true as the only option, it can be customized using replyQueueOptions.
  • It is recommanded to use a random string generator and set the queue option to exclusive to be able to create more than one client application instance (in the case of using a load balancer for example) and prevent the round-robin nature of rabbitmq to send a message to an instance that didn't make a request.
  • The queueOptions is no longer available on RabbitMQClient constructor and has been replaced with replyQueueOptions which is more explicit.

How to use

Sending messages

From 0.3.1, it is possible to configure the message options and send them along the message itself by sending an RMQMessage instead of a string. This allows to use specific options per message.

import { RMQMessage } from '@lukadriel/nestjs-rabbitmq-transporter/dist/interfaces/rmq-options.interface';
import { Controller, Get, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';

@Controller()
export class AppController {
  constructor(
    @Inject('RABBITMQ_CLIENT') private readonly rabbitmqClient: ClientProxy,
  ) {}

  @Get()
  getHello() {
    const msg: RMQMessage = {
      content: 'hey there',
      options: {
        persistent: true,
      },
    };
    this.rabbitmqClient.send('hello', msg).subscribe((data) => {
      console.log('response: ', data);
    });
    return 'message sent';
  }
}

This also apply to the server response which can return an RMQMessage.

import { RMQMessage } from '@lukadriel/nestjs-rabbitmq-transporter/dist/interfaces/rmq-options.interface';
import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @MessagePattern('hello')
  getHello(data: any) {
    console.log('received from client: ', data);
    const response: RMQMessage = {
      content: data,
      options: {
        persistent: true,
      },
    };
    return response;
  }
}

Microservices

Server

    import { NestFactory } from '@nestjs/core';
    import { MicroserviceOptions } from '@nestjs/microservices';
    import { AppModule } from './app.module';
    import {
    RabbitMQServer,
    ExchangeType,
    } from '@lukadriel/nestjs-rabbitmq-transporter';

    async function bootstrap() {
        const app = await NestFactory.createMicroservice<MicroserviceOptions>(
            AppModule,
            {
                strategy: new RabbitMQServer({
                    queue: 'queue_name',
                    exchange: 'exchange_name',
                    exchangeType: ExchangeType.TOPIC,
                    urls: ['amqp://localhost:5672'],
                    noAck: true,
                }),
            }
        );
        app.listen();
    }
    bootstrap();

Client

Add a new provider in a module

    providers: [
        {
            provide: 'RABBITMQ_CLIENT',
            useFactory: () => {
                return new RabbitMQClient({
                    urls: ['amqp://localhost:5672'],
                    exchange: 'exchange_name',
                    exchangeType: ExchangeType.TOPIC,
                    queue: 'server_queue_name',
                    replyQueue: 'client_queue_name',
                    replyQueueOptions: {
                        exclusive: true,
                    }
                    noAck: true,
                });
            },
        },
    ],

Inject the provider where you want to use it

    constructor(@Inject('RABBITMQ_CLIENT') private readonly client: ClientProxy){}

Emitting event works as usual. All events will go through the defined exchange topic.

this.client.emit('pattern', { data: 'how are you' });

For the request-response pattern, the request are sent to a specific queue defined by the pattern 'queue_name/pattern'

    this.client.send('queue_name/pattern', { data: 'how are you' });

Hybrid Applications

Server

    import { NestFactory } from '@nestjs/core';
    import { MicroserviceOptions } from '@nestjs/microservices';
    import { AppModule } from './app.module';
    import {
    RabbitMQServer,
    ExchangeType,
    } from '@lukadriel/nestjs-rabbitmq-transporter';

    async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.connectMicroservice<MicroserviceOptions>({
        strategy: new RabbitMQServer({
        queue: 'queue_name',
        exchange: 'exchange_name',
        exchangeType: ExchangeType.TOPIC,
        urls: ['amqp://localhost:5672'],
        noAck: true,
        }),
    });
    await app.startAllMicroservicesAsync();
    await app.listen(3000);
    }
    bootstrap();

Client

Same as for the microservice applications