@kaushverse/rabbitmq-core
v1.1.4
Published
Reusable RabbitMQ (AMQP / AMQPS) core for Node.js & microservices
Maintainers
Readme
@kaushverse/rabbitmq-core
Production-ready RabbitMQ SDK for Node.js microservices.
This package provides a clean and opinionated way to connect, publish, consume, and monitor RabbitMQ, while handling TLS, logging, health checks, and graceful shutdown automatically.
Features
- Single RabbitMQ connection per service
- Supports
amqp://andamqps:// - Message publishing and consuming helpers
- Built-in bootstrap for service lifecycle
- Structured logger (pretty logs in dev, JSON in prod)
- Health check helper for monitoring
- Graceful shutdown (SIGINT / SIGTERM)
- Framework agnostic (Express, Fastify, NestJS)
Installation
npm install @kaushverse/rabbitmq-coreFull Express Service Example
Below is a complete example showing how to use @kaushverse/rabbitmq-core
inside an Express-based microservice with health checks and publishing routes.
Example: order-service
import "dotenv/config";
import express from "express";
import { bootstrap, rabbitHealth } from "@kaushverse/rabbitmq-core";
const app = express();
app.use(express.json());
// Health check endpoint
app.get("/health", (_, res) => {
res.json({
status: "ok",
...rabbitHealth(),
});
});
// Bootstrap the service
bootstrap({
serviceName: "order-service",
rabbit: {
url: process.env.RABBITMQ_URL!,
tls: {
caPath: process.env.RABBITMQ_CA_PATH,
servername: process.env.RABBITMQ_SERVERNAME,
rejectUnauthorized: false, // set true in production
},
},
start: () => {
app.listen(3000, () => {
console.log("🚀 API running on port 3000");
});
},
});RABBITMQ_URL=amqps://user:[email protected]:5671/vhost
RABBITMQ_CA_PATH=./certs/ca.pem
RABBITMQ_SERVERNAME=mq.example.comPublishing Messages
Use publishMessage to publish events to RabbitMQ.
The SDK automatically handles channels, serialization, and persistence.
Import
import { publishMessage } from "@kaushverse/rabbitmq-core";
await publishMessage({
exchange: "order.events",
type: "topic",
routingKey: "order.created",
message: {
orderId: "ORD-123",
amount: 499,
},
headers: {
source: "order-service",
},
});