npm package: @kaushverse/rabbitmq-core
Description: Reusable RabbitMQ (AMQP / AMQPS) core for Node.js & microservices
Author: Kaushik
Latest version: 1.1.7

# @kaushverse/rabbitmq-core 🐇

🚀 **Production-ready RabbitMQ SDK for Node.js microservices**

`@kaushverse/rabbitmq-core` provides a **clean, opinionated abstraction** over RabbitMQ for Node.js services.

It handles **connections, publishing, consuming, health checks, TLS, logging, and graceful shutdown** — automatically — so you can focus on business logic, not infra glue.

---

## ✨ Features

- 🔗 **Single RabbitMQ connection per service**
- 🔐 Supports `amqp://` and `amqps://` (TLS)
- 📤 Easy message publishing helpers
- 📥 Consumer utilities with retry control
- ❤️ Built-in RabbitMQ health check
- 🧼 Graceful shutdown (`SIGINT`, `SIGTERM`)
- 📦 Framework-agnostic (Express, Fastify, NestJS)
- 📊 Pretty logs in dev, structured JSON logs in prod

---

## 📦 Installation

```bash
npm install @kaushverse/rabbitmq-core
```

## 🏗️ Full Express Service Example

### Example: `order-service`

This example shows how to bootstrap a production-ready Express service using  
`@kaushverse/rabbitmq-core`.

---

### 📁 `src/index.js`

```ts
import "dotenv/config";
import { bootstrap } from "@kaushverse/rabbitmq-core";
import { startServer } from "./server.js";

bootstrap({
  serviceName: "order-service",

  rabbit: {
    url: process.env.RABBITMQ_URL!,
    tls: {
      caPath: process.env.RABBITMQ_CA_PATH,
      servername: process.env.RABBITMQ_SERVERNAME,
      rejectUnauthorized: true,
    },
  },

  start: startServer,
});
```
### 📁 src/server.js

```ts
import express from "express";
import { rabbitHealth } from "@kaushverse/rabbitmq-core";
import publishRoutes from "./routes/publish.routes.js";

export function startServer() {
  const app = express();
  app.use(express.json());

  app.get("/health", (_, res) => {
    res.json({
      status: "ok",
      rabbit: rabbitHealth(),
    });
  });

  app.use("/publish", publishRoutes);

  app.listen(3000, () => {
    console.log("🚀 API running on port 3000");
  });
}
```

## 📤 Publishing Messages

Use publishMessage to emit events to RabbitMQ.

Channels, serialization, exchange declaration, and message persistence are handled automatically.

```ts
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",
  },
});
```

## 🔐 Environment Variables
RABBITMQ_URL=amqps://user:password@mq.example.com:5671/vhost
RABBITMQ_CA_PATH=./certs/ca.pem
RABBITMQ_SERVERNAME=mq.example.com
PORT=3000

## ❤️ Health Check

Built-in RabbitMQ health reporting.

```ts
import { rabbitHealth } from "@kaushverse/rabbitmq-core";

app.get("/health", (_, res) => {
  res.json({
    status: "ok",
    rabbit: rabbitHealth(),
  });
});
```

## Example Response

{
  "status": "ok",
  "rabbit": {
    "connected": true,
    "url": "amqps://mq.example.com"
  }
}

## 🛡️ Production Best Practices

✅ Always use amqps://

🔐 Keep rejectUnauthorized: true

🔗 One service = one RabbitMQ connection

🔁 Handle retries at consumer level

🧾 Use dedicated users & vhosts per service

📊 Monitor connections & queues via RabbitMQ Management UI