Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

rabbitmq-messages

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rabbitmq-messages

RabbitMQ RPC library

Source
npmnpm
Version
0.3.1
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

RabbitMQ RPC library

This library will take care of RPC requests and messaging between microservices. It is easy to bind to our existing controllers to RMQ routes. You can use it with any NodeJS framework.

Start

First, install the package:

npm i rabbitmq-messages

Then you need to extend your controller with RMQController:

import { RMQController, RMQRoute } from 'rabbitmq-messages';

export class AppController extends RMQController {
    constructor() {
        super({
            exchangeName: 'my_exchange',
            connections: [{
                login: 'admin',
                password: 'admin',
                host: 'localhost',
            }],
        });
    }
}

In super() you pass connection options =:

  • exchangeName (string) - Exchange that will be used to send messages to.
  • connections (Object[]) - Array of connection parameters. You can use RQM cluster by using multiple connections.

Additionally, you can use optional parameters:

  • queueName (string) - Queue name which your microservice would listen and bind topics specified in subscriptions to this queue. If this parameter is not specified, your microservice could send messages and listen to reply or send notifications, but it couldn't get messages or notifications from other services.
  • subscriptions (string[]) - Message topics your microservice will subscribe to. It will receive messages only with these topics. Full connection example:
super({
  exchangeName: 'my_exchange',
  connections: [{
      login: 'admin',
      password: 'admin',
      host: 'localhost',
  }],
  queueName: 'my-service-queue',
  subscriptions: [
    'sum.rpc',
    'info.none'
  ],
})
  • prefetchCount (boolean) - You can read more here.
  • isGlobalPrefetchCount (boolean) - You can read more here.
  • reconnectTimeInSeconds (number) - Time in seconds before reconnection retry. Default is 5 seconds.
  • queueArguments (object) - You can read more about queue parameters here.

Sending messages

To send message with RPC topic use send() method in your controller or service:

// In TypeScript
this.send<number[], number>('sum.rpc', [1, 2, 3]);
// Or in JavaScript
this.send('sum.rpc', [1, 2, 3]);

This method returns a Promise. First type - is a type you send, and the second - you recive.

  • 'sum.rpc' - name of subscription topic that you are sending to.
  • [1, 2, 3] - data payload. To get a reply:
this.send('sum.rpc', [1, 2, 3]).then(reply => {
    //...
});

If you want to just notify services:

// In TypeScript
this.notify<string>('info.none', 'My data');
// Or in JavaScript
this.notify('info.none', 'My data');

This method returns a Promise.

  • 'info.none' - name of subscription topic that you are notifying.
  • 'My data' - data payload.

Recieving messages

To listen for messages bind your controller methods to subscription topics with RMQRoute() decorator:

export class AppController extends RMQController {

    //...

    @RMQRoute('sum.rpc')
    sum(numbers: number[]): number {
        return numbers.reduce((a, b) => a + b, 0);
    }

    @RMQRoute('info.none')
    info(data: string) {
        console.log(data);
    }
}

Return value will be send back as a reply in RPC topic. In 'sum.rpc' example it will send sum of array values. And sender will get 6:

this.send('sum.rpc', [1, 2, 3]).then(reply => {
    // reply: 6
});

Keywords

rabbitmq

FAQs

Package last updated on 20 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts