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

@thecolvinco/nodejs-amqplib

v2.0.1

Published

RabbitMQ abstraction with some utils

Downloads

3

Readme

Colvin nodejs async events

A RabbitMQ package with some utils like retryables

Usage

This library give you some classes, functions, types and interfaces ready to use. The idea behind this project has been inspired from messenger Symfony component so we have to define a config file and a couple of files. Let's see how to start it with.

  • Create a config file with a required structure
  • Setup your transports and bind the events
  • Create your events and commands with their respective handlers
  • Create your consumers

Create a config file with a required structure

You have to create a file with this structure. Later you must to import this file so you can put it in any place. Let's see an example explained

export default {
  transports: {
    accounting_commands: { // A transport unique name as identifier
      connectionString: process.env.RABBITMQ_CONNECTION_URL, // A dns connection string like --> amqp://rabbitmq:rabbitmq@rabbitmq-blom:5672
      exchange: { // the exchange for this transport where all message will be send it
        name: 'blom.accounting.commands.exchange', 
        type: 'fanout',
      },
      queues: [ 
        {
          name: 'blom.accounting.commands', // A queue name
          bindingKey: null, // A queue binding key. You can use null for fanout exchanges or wildcard string for topic ones
          options: { // All allowed queue options provided for the ampq node lib
            durable: true,
            deadLetterExchange: 'blom.accounting.exchange.dlx',
            deadLetterRoutingKey: 'blom.accounting.commands.dlx',
          },
          retryPolicy: { // The retries policiy for this trasnport. The retries will be try in a exponential way (firstly in 2s, secondly in 4s, lastly in 6s)
            maxRetries: 3,
            delay: 2000,
          },
        },
      ],
    },
    // <----- others trasnports goes here
  },
  subscribers: {
    domainEvents: [ // A mandatory key. All your domains events config goes under it
      {
        eventName: 'blom.superapp.1.domain_event.order.updated', // The event which the built-in domain events subscribers will subscribe it
        commandsPath: [ // An array of commands paths witch will be created and dispached. These events (in this case commands) must implement the EventInterface
          'server/events/orders/command/SyncronizeOrderCommand.js',
          'server/events/orders/command/SendOrderNotificationCommand.js',
        ],
      },
      // <--- Other events goes here
    ],
    commands: [
      {
        eventName: 'blom.accounting.1.command.order.syncronize_order', // The event which the built-in command subscribers will subscribe it
        handlerPath: 'server/events/orders/command/SyncronizeOrderCommandHandler.js', // The handler which will be invoke passing their Command as argument
      },
      // <--- Other commands goes here
    ],
  },
  routing: { // A map of events wich will can dispatch through a specific transports
    SyncronizeOrderCommand: { // The key is the name of the class ([instance].constructor.name)
      transport: 'accounting_commands',
    },
    // <--- Other events goes here
  },
};

Setup your transports and bind the events

There are two functions for setting up. You should use this functions before app init.

import { pubSubInitialization, transportsInitialization } from '@thecolvinco/nodejs-amqplib';
import config from 'path-to-your-config-file';

  const {
    eventDispatcher,
    domainEventEmitter,
    commandEmitter,
  } = pubSubInitialization({ config });

  transportsInitialization({ config });

Create your events and commands with their respective handlers

This is an example for a Command

export default class SendOrderNotificationCommand {
  constructor({ message }) {
    this.message = message;
  }

  getName() {
    return 'blom.accounting.1.command.order.send_notification';
  }

  getPayload() {
    return this.message.data.attributes;
  }

  getCorrelationId() {
    return this.message.data.messageId || null;
  }
}

This is an example for a command handler

export default class SendOrderNotificationCommandHandler {
  constructor({ command }) {
    this.command = command;
  }

  handle() {
    console.log('Do something!');
  }
}

Create your consumers

import { CommandConsumer } from '@thecolvinco/nodejs-amqplib';
import config from 'path-to-your-config';

const commandConsumer = new CommandConsumer({ config });

commandConsumer.consume({
  transport: 'accounting_commands',
  queueName: 'blom.accounting.commands', 
}).then(() => {
  console.info('Waiting for messages....');
}).catch(error => {
  console.error(error.message);
  process.exit();
});

About

This package is maintained by TheColvinCo

LICENSE

Code is licensed under the MIT License.