npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@heavybit/queue-publisher-package

v1.0.5

Published

Shared queue publisher package that allows microservices to publish to Rabbit MQ queues

Downloads

28

Readme

Introduction

The RabbitMQPublisher package is a lightweight Node.js library designed to simplify message publishing to RabbitMQ queues. This package abstracts the complexities of establishing a RabbitMQ connection, creating channels, and managing queues, allowing developers to integrate RabbitMQ into their microservices with minimal effort.

Features

  • Persistent RabbitMQ connection management.
  • Automatic queue declaration and durable message publishing.
  • Graceful handling of connection and channel closure.
  • Built-in error handling for connection and message publishing.
  • Simple integration into Node.js microservices.

Technologies Used

  • Node.js: The core runtime environment for building the package.
  • RabbitMQ: A message broker for sending messages between microservices.
  • amqplib: A Node.js library for working with RabbitMQ.

Installation

You can install the package via npm by running the following command:

npm install @heavybit/queue-publisher-package

Requirements

  • Node.js version 12 or higher.
  • RabbitMQ installed locally or accessible via a cloud instance.

Usage

Here’s how to integrate the RabbitMQPublisher package into your project:

  1. Import the Package First, import the RabbitMQPublisher class into your Node.js application:

const RabbitMQPublisher = require('@heavybit/queue-publisher-package');

  1. Initialize the Publisher Create an instance of the RabbitMQPublisher:

const publisher = new RabbitMQPublisher();

  1. Publish Messages to a Queue You can easily publish messages using the publishToQueue() method. The method handles establishing a connection, asserting the queue, and sending the message.
const queueName = 'taskQueue';
const message = { task: 'Process Order', orderId: 12345 };

(async () => {
  try {
    await publisher.publishToQueue(queueName, message);
    console.log('Message sent successfully');
  } catch (error) {
    console.error('Failed to send message:', error);
  } finally {
    await publisher.close(); // Gracefully close the connection
  }
})();
  1. Close the Connection Always ensure to close the connection and channel once you are done publishing messages:
(async () => {
  try {
    await publisher.close();
    console.log('RabbitMQ connection closed');
  } catch (error) {
    console.error('Failed to close connection:', error);
  }
})();

Methods

async connect(rabbitMQUrl)

Establishes a connection to RabbitMQ and creates a channel. By default, the package uses the RABBITMQ_URL environment variable or defaults to amqp://rabbitmq.

await publisher.connect('amqp://custom-url');

async publishToQueue(queue, message)

Ensures that the RabbitMQ connection is established and the specified queue is asserted, before sending the message as a persistent JSON message.

await publisher.publishToQueue('myQueue', { key: 'value' });

async close()

Closes both the RabbitMQ channel and connection gracefully.

await publisher.close();

Environment Configuration

By default, the package uses the following environment variable to configure the RabbitMQ connection:

RABBITMQ_URL: The RabbitMQ URL, which defaults to amqp://rabbitmq if not set.

Example .env configuration:

RABBITMQ_URL=amqp://your-rabbitmq-url

Error Handling

The package includes robust error handling mechanisms:

If a connection to RabbitMQ fails, an error is thrown and logged. If message publishing fails, the error is caught, logged, and rethrown. The close() method gracefully handles errors during the closure of RabbitMQ connections and channels.

Example:

(async () => {
  try {
    await publisher.publishToQueue('queue', { message: 'Hello World' });
  } catch (error) {
    console.error('Error:', error.message);
  }
})();

Example: Integrating into a Microservice

Here’s an example of how you can integrate the RabbitMQPublisher into an Express-based microservice to publish messages to a queue:

const express = require('express');
const RabbitMQPublisher = require('@heavybit/queue-publisher-package');

const app = express();
const publisher = new RabbitMQPublisher();

// Middleware to parse JSON bodies
app.use(express.json());

app.post('/publish', async (req, res) => {
  const { queue, message } = req.body;

  try {
    await publisher.publishToQueue(queue, message);
    res.status(200).send('Message sent to RabbitMQ');
  } catch (error) {
    console.error('Failed to send message:', error);
    res.status(500).send('Failed to send message');
  }
});

app.listen(3000, () => {
  console.log('Microservice is running on port 3000');
});