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

@kazinaimul/rabbitmq

v1.0.3

Published

This package will help to connect RabbitMQ, publish and consume messages

Downloads

17

Readme

RabbitMQ Helper

Installation

To integrate the RabbitMQ Helper package into your project, use the following command:

yarn add @kazinaimul/rabbitmq

Usage

Establishing Connection

In your Express app's app.js file, establish a connection to RabbitMQ using the following code:

import { RabbitMQConnection } from '@kazinaimul/rabbitmq';

RabbitMQConnection.connect('RABBITMQ_CONNECTION_URL');

Replace RABBITMQ_CONNECTION_URL with your RabbitMQ server connection URL, formatted as amqp://username:password@localhost:5672.

Publishing a Message

To publish a message, create a Publisher class by extending the provided Publisher class from the package. Here is an example:

import { Publisher } from '@kazinaimul/rabbitmq';

export class PublishMessage extends Publisher {
    constructor() {
        const queueName = 'queue-name';
        super(queueName);
    }

    async publish<MessageType>(message: MessageType): Promise<void> {
        try {
            const customOptions = {
                exchange: `Exchange_name`,
                routingKey: queue,
                delay: 0,
                exchangeType: "direct",
                headers: {},
            }; // Custom optional values, overwriting default options
            await this.rabbitMQClient.publish(this.queueName, message, customOptions);
        } catch (error) {
            console.error('Error publishing messages:', error);
        }
    }
}

By default, the package uses the following options to publish a message:

const defaultOptions = {
    exchange: `Exchange_${queue}`,
    routingKey: queue,
    delay: 0,
    exchangeType: "direct",
    headers: {},
};

You can customize these options as needed.

You can then use this class to publish messages anywhere in your application:

const publisher = new PublishMessage();
publisher.publish(publishJson);

Consuming Messages

To consume messages, create a Consumer class by extending the provided Consumer class from the package. Here is an example:

import { Consumer } from '@kazinaimul/rabbitmq';

export class MyConsumer extends Consumer {
    constructor() {
        const queueName = 'queue-name';
        const options = {
            retry: true, // If true, messages will be queued again in case of an error (default is true)
            retry_count: 3, // Maximum retry count, beyond which the message will be moved to an error queue
            retry_delay: 0 // Delay in milliseconds for retries
        };
        super(queueName, options); // Options is an optional field
    }

    async execute<T extends object>(message: T): Promise<void> {
        // Implement your own logic to handle the consumed message
    }
}

Register your consumers in a file (e.g., consumerRegister.ts):

import { MyConsumer } from './MyConsumer';

export const consumerRegister: any[] = [new MyConsumer()];

In your application, consume messages by iterating through the registered consumers:

import { consumerRegister } from './consumerRegister';

// Consume messages from registered consumers
for (const consumer of consumerRegister) {
    consumer.consume();
}

Now, the MyConsumer class will process messages from the specified queue.

Remember to replace 'queue-name' with the actual queue name you want to use.

Notes

  • Replace placeholder values and adjust configurations according to your RabbitMQ setup.
  • For further customization, refer to the provided classes in the package and their methods.

Feel free to reach out if you encounter any issues or have questions related to this package. Happy coding!