FireRabbit is a simple, lightweight wrapper for RabbitMQ using the amqplib
library. It simplifies sending and receiving messages with minimal configuration, making it ideal for quick integrations and scalable messaging solutions.
Install FireRabbit via npm:
npm install @marcuwynu23/frbjs
- Easy connection initialization with RabbitMQ.
- Simple methods to send and receive messages.
- Handles JSON messages seamlessly.
- Graceful connection and channel closure.
Create a new instance of the FireRabbit class and initialize the RabbitMQ connection.
const FireRabbit = require('@marcuwynu23/frbjs');
(async () => {
const rabbit = new FireRabbit();
await rabbit.init('amqp://localhost');
})();
Send a message to a specified RabbitMQ queue.
const FireRabbit = require('@marcuwynu23/frbjs');
(async () => {
const rabbit = new FireRabbit();
await rabbit.init('amqp://localhost');
const queueName = 'test-queue';
const message = { id: 1, text: 'Hello, RabbitMQ!' };
await rabbit.send(queueName, message);
await rabbit.close();
})();
Listen to a RabbitMQ queue and process incoming messages.
const FireRabbit = require('@marcuwynu23/frbjs');
(async () => {
const rabbit = new FireRabbit();
await rabbit.init('amqp://localhost');
const queueName = 'test-queue';
const message = await rabbit.receive(queueName);
console.log("Received message:", message);
})();
Close the RabbitMQ connection gracefully:
await rabbit.close();
You can use environment variables to configure the RabbitMQ URI:
RABBIT_MQ_URI
: The URI for the RabbitMQ server (e.g.,amqp://localhost
).
Ensure you handle errors properly, especially when initializing the connection or interacting with RabbitMQ. For example:
try {
await rabbit.init('amqp://localhost');
} catch (error) {
console.error('Failed to initialize RabbitMQ:', error);
}
FireRabbit is open-source software licensed under the MIT License.
Happy messaging with FireRabbit!