1.2.8 • Published 6 months ago

node-rabbitmq-manager v1.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

Getting the RabbitMQ Instance

You can obtain the underlying RabbitMQ instance from the node-rabbitmq-manager package. Here's how to do it:

First, import the Manager class from the package into your application:

import { Manager } from "node-rabbitmq-manager";

Next, create an instance of the Manager by providing the RabbitMQ server URL and optional configuration parameters. You can also set the maximum number of retry attempts and the reconnection timeout.

const rabbitmqManager = new Manager(
  "amqp://localhost", // RabbitMQ server URL
  5, // Maximum retry attempts (optional)
  5000 // Reconnection timeout in milliseconds (optional)
);

To start the RabbitMQ connection, use the start method as described in the previous section:

rabbitmqManager
  .start()
  .then(() => {
    console.log("Connected to RabbitMQ server.");
  })
  .catch((error) => {
    console.error("Error connecting to RabbitMQ server:", error);
  });

Once you have started the connection, you can access the RabbitMQ instance by using the client property of the rabbitmqManager object. This property provides direct access to the RabbitMQ instance created by the amqplib library:

const rabbitmqConnection = rabbitmqManager.client;

// Now you can use 'rabbitmqConnection' to work with the RabbitMQ instance directly.
// For example, you can create channels, publish messages, and subscribe to queues using the 'amqplib' methods.

This allows you to interact with the RabbitMQ instance using the full set of methods provided by the amqplib library.

Events

The RabbitMQManager emits events to notify you of various connection states and errors. You can listen to these events to handle specific scenarios in your application.

  • open: Emitted when the connection to the RabbitMQ server is established.
  • closed: Emitted when the connection is closed, and the controller is attempting to reconnect.
  • reconnecting: Emitted when the controller is in the process of reconnecting.
  • connecting: Emitted when the controller is attempting to connect to the RabbitMQ server.
  • error: Emitted when an error occurs during the connection process.

You can use the on method to listen to these events and respond to them in your code:

RabbitMQManager.on("open", (manager) => {
  console.log("RabbitMQ connection is open.");
});

RabbitMQManager.on("connecting", (manager) => {
  console.log("RabbitMQ connection is in the process of connecting.");
});

RabbitMQManager.on("error", (manager, error) => {
  console.error("RabbitMQ error:", error);
});

Mods

The RabbitMQManager offers some mods that not in amqplib such as getting all the client data. so in this manager it has built in functions to get that data.

const rabbitmqManager = new Manager(
  "amqp://localhost", // RabbitMQ server URL
  5, // Maximum retry attempts (optional)
  5000, // Reconnection timeout in milliseconds (optional)
  mods:{
    enabled:true,
    management: {
        port: 15672, // Rabbitmq Management port
        url:  "http://localhost", // Rabbitmq Management url
        username: null,// Rabbitmq Management username 
        password: null,// Rabbitmq Management password 
    }
  }
);

License

This package is released under the MIT License. See the LICENSE file for details.

1.2.8

6 months ago

1.2.7

6 months ago

1.2.6

6 months ago

1.2.5

6 months ago

1.2.4

6 months ago

1.2.3

6 months ago

1.2.2

6 months ago

1.2.1

6 months ago

1.2.0

6 months ago

1.1.9

6 months ago

1.1.8

6 months ago

1.1.7

6 months ago

1.1.6

6 months ago

1.1.5

6 months ago

1.1.4

6 months ago

1.1.3

6 months ago

1.1.2

6 months ago

1.1.1

6 months ago

1.1.0

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago