Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kazi-naimul/rabbitmq
RabbitMQ Helper: Simplifying RabbitMQ integration in Node.js. Easily connect, publish messages, and consume with easy retry mechanism.
https://github.com/kazi-naimul/rabbitmq
javascript nodejs npm-package rabbbitmq rabbitmq-consumer rabbitmq-helper rabbitmq-publisher typescript
Last synced: 9 days ago
JSON representation
RabbitMQ Helper: Simplifying RabbitMQ integration in Node.js. Easily connect, publish messages, and consume with easy retry mechanism.
- Host: GitHub
- URL: https://github.com/kazi-naimul/rabbitmq
- Owner: kazi-naimul
- Created: 2023-08-06T17:46:48.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-06T14:58:00.000Z (11 months ago)
- Last Synced: 2024-09-15T22:44:14.540Z (2 months ago)
- Topics: javascript, nodejs, npm-package, rabbbitmq, rabbitmq-consumer, rabbitmq-helper, rabbitmq-publisher, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@kazinaimul/rabbitmq
- Size: 30.3 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# RabbitMQ Helper
## Installation
To integrate the RabbitMQ Helper package into your project, use the following command:
```bash
yarn add @kazinaimul/rabbitmq
```## RabbitMQ Server
You can use following docker file to create a RabbitMQ server:
```
FROM rabbitmq:3.12.0-managementRUN apt-get update
RUN apt-get install -y curl
RUN curl -L https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.12.0/rabbitmq_delayed_message_exchange-3.12.0.ez > $RABBITMQ_HOME/plugins/rabbitmq_delayed_message_exchange-3.12.0.ez
RUN chown rabbitmq:rabbitmq $RABBITMQ_HOME/plugins/rabbitmq_delayed_message_exchange-3.12.0.ez
RUN rabbitmq-plugins enable --offline rabbitmq_delayed_message_exchange
RUN rabbitmq-plugins enable --offline rabbitmq_consistent_hash_exchange
```
# To be noted this package requires following plugins to enabled:
rabbitmq_delayed_message_exchange
rabbitmq_consistent_hash_exchange
## Usage### Establishing Connection
In your Express app's `app.js` file, establish a connection to RabbitMQ using the following code:
```javascript
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:
```javascript
import { Publisher } from '@kazinaimul/rabbitmq';export class PublishMessage extends Publisher {
constructor() {
const queueName = 'queue-name';
super(queueName);
}async publish(message: MessageType): Promise {
try {
const customOptions = {
exchange: `your-exchange_name`,
routingKey: queueName,
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:
```javascript
const defaultOptions = {
exchange: `Exchange_${queueName}`,
routingKey: queueName,
delay: 0,
exchangeType: "direct",
headers: {},
};
```
Here queueName is the given Queue Name while initialize the class.
You can customize these options as needed.You can then use this class to publish messages anywhere in your application:
```javascript
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:
```javascript
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(message: T): Promise {
// Implement your own logic to handle the consumed message
}
}
```Register your consumers in a file (e.g., `consumerRegister.ts`):
```javascript
import { MyConsumer } from './MyConsumer';export const consumerRegister: any[] = [new MyConsumer()];
```In your application, consume messages by iterating through the registered consumers:
```javascript
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!