开发者社区> 镜像站> NPM> mq-connection
mq-connection
Cached connections to an AMQP server.
Last updated 5 years ago by philcockfield .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mq-connection 
SYNC missed versions from official npm registry.

mq-connection

Build Status

Cached, idempotent connections to an AMQP/RabbitMQ server.


RabbitMQ connections are expensive to create and destroy. You want avoid having too many open connections, in contrast to channels which you can (and should) have many of over a single open channel (ref).

This module allows you to work with a single cached connection for each server URL. This is useful when you are developing different modules that connect to RabbitMQ, and don't wish to explicitly pass connections around between API's.

See also:

Installation

npm install --save mq-connection

Usage

import connect from "mq-connection";

connect("amqp://rabbitmq")
  .then(connection => {
    // An `amqplib` connection object is returned.
  });

connect("amqp://rabbitmq")
  .then(connection => {
    // This `connection` is the same (cached) connection that was returned above.
  });

The amqp connection that is returned has a modified close() method that returns a promise, allowing you to determine exactly when the connection has successfully closed.

connection.close()
  .then(result => {
    // The connection is now closed.
  });

Once closed the connection is removed from the cache.

Fakes

You can easily return fake connections for the purposes of testing using the fake() method:

import connect from "mq-connection";
connect.fake();

connect("amqp://whatever")
  .then(connection => {
    // This is a {FakeConnection}.
  });

To restore the module to make real connections use the 'real()' method:

import connect from "mq-connection";
connect.fake(); // Makes fake connections.
...
connect.real(); // Back to making real connections now.

The fake API's are being fleshed out as needed. If a method that you need is not there feel free to add it and submit a pull-request.

Tests

npm test

License: MIT

Current Tags

  • 1.1.3                                ...           latest (5 years ago)

9 Versions

  • 1.1.3                                ...           5 years ago
  • 1.1.2                                ...           5 years ago
  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           5 years ago
  • 1.0.3                                ...           5 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.0.1                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (3)
Dev Dependencies (5)
Dependents (1)