Registry / messaging / rabbitmq-with-retry-and-dlq

rabbitmq-with-retry-and-dlq

JSON →
library1.0.27jsnpmunverified

A production-ready TypeScript RabbitMQ library (v1.0.27) that enforces industry best practice separation of concerns (Publisher→Exchanges, Consumer→Queues) with built-in automatic exponential backoff retry and dead letter queue routing after max retries. Features lazy loading (automatic connection on first use), auto-reconnection, support for direct/topic/fanout/headers exchanges, and full TypeScript types. Released on npm with a monthly cadence. Differentiators: explicit retry config per queue, event emitter interface, and no direct publisher-to-queue API (prevents anti-patterns). Requires Node >=16 and a running RabbitMQ instance.

npm install rabbitmq-with-retry-and-dlq
INSTALL
IMPORT
SIG · RABBITMQ-WITH-RETR
R
rabbitmq-with-retry-and-dlq
messagingjavascriptv1.0.27
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

initializeRabbitMQ
import { initializeRabbitMQ } from 'rabbitmq-with-retry-and-dlq'
const { initializeRabbitMQ } = require('rabbitmq-with-retry-and-dlq')
ESM-only package; does not support CommonJS require().
publisher
import { publisher } from 'rabbitmq-with-retry-and-dlq'
publisher is a singleton object, not a class. Methods: assertExchange, publishToExchange, publishToQueue (discouraged).
consumer
import { consumer } from 'rabbitmq-with-retry-and-dlq'
import { Consumer } from 'rabbitmq-with-retry-and-dlq'
consumer is a singleton object. No Consumer class exported.
isPublisherConnected
import { isPublisherConnected } from 'rabbitmq-with-retry-and-dlq'
Returns boolean; does not throw.
EventEmitter
import { EventEmitter } from 'rabbitmq-with-retry-and-dlq'
import { EventEmitter } from 'events'
Library exports its own EventEmitter for consuming events.

Demonstrates initialization, exchange assertion, queue setup with retry config, consuming, and publishing a message.

import { initializeRabbitMQ, publisher, consumer } from 'rabbitmq-with-retry-and-dlq'; async function main() { await initializeRabbitMQ(process.env.RABBITMQ_URL ?? 'amqp://localhost:5672'); await publisher.assertExchange('orders', { exchangeType: 'topic' }); await consumer.setupQueue({ queueName: 'orders-processor', exchangeName: 'orders', exchangeType: 'topic', routingKeys: ['order.created', 'order.updated'], retryConfig: { maxRetries: 5, retryDelayMs: 2000 }, }); await consumer.consumeQueue({ queueName: 'orders-processor', onMessage: async (message) => { console.log('Received:', message.content.toString()); }, }); // Publish a message await publisher.publishToExchange({ exchangeName: 'orders', routingKey: 'order.created', message: { orderId: 123 }, }); } main().catch(console.error);
Debug
Known issues
breakinginitializeRabbitMQ must be called and awaited exactly once before using publisher or consumer.
fix
Call await initializeRabbitMQ(url) at application startup. Do not call it multiple times.
affects: >=1.0.0
gotchapublisher.publishToQueue() is supported but heavily discouraged per library docs. Use publishToExchange() instead to follow best practices.
fix
Use publisher.assertExchange() and publisher.publishToExchange() instead of direct queue publishing.
affects: >=1.0.0
gotchaconsumer.consumeQueue() does not return a subscription handle; you cannot stop consuming via the returned object.
fix
Use consumer.cancelConsumer(queueName) to stop consuming, or use the EventEmitter pattern.
affects: >=1.0.0
gotchaThe library uses a singleton connection. Re-initializing with a different URL after the first call has no effect.
fix
Only call initializeRabbitMQ once. Use environment variables for dynamic config.
affects: >=1.0.0
gotchaRequires Node >=16 due to fs/promises and other ESM features.
fix
Upgrade Node.js to version 16 or higher.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'connection')
Using publisher or consumer before calling and awaiting initializeRabbitMQ.
fix
Ensure await initializeRabbitMQ(url) is called first in your application startup.
Error: connect ECONNREFUSED ::1:5672
RabbitMQ not running on localhost or wrong URL.
fix
Start RabbitMQ (e.g., docker run -d --name rabbitmq -p 5672:5672 rabbitmq:3) or provide correct URL via RABBITMQ_URL env var.
TypeError: consumer.consumeQueue is not a function
Importing consumer incorrectly (e.g., import { Consumer } instead of { consumer }).
fix
Use import { consumer } from 'rabbitmq-with-retry-and-dlq' (lowercase).
Error: Channel closed by server: 406 (PRECONDITION-FAILED) with message "PRECONDITION_FAILED - inequivalent arg 'x-message-ttl' ..."
Trying to setup a queue with different arguments than an existing queue.
fix
Delete the existing queue or use consistent queue setup arguments across restarts.
Upgrade
Version history
1.0.27latest on npm
Audit
Dependencies
amqplibrequiredAMQP 0-9-1 client for RabbitMQ communication
Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
2
Amazon
1
Resources
rabbitmq-with-retry-and-dlq — npm install rabbitmq-with-retry-and-dlq · libregistry