fastify-amqp-async
v1.0.1
Published
Fastify AMQP plugin, which uses amqplib-as-promised so as to allow for Promise-based RabbitMQ operations.
Downloads
595
Maintainers
Readme
Fastify AMQP plugin with a modern, promise-based API
fastify-amqp-async is a Fastify plugin inspired by fastify-amqp which allows for interacting with RabbitMQ using a more modern, Promise-based API provided by amqplib-as-promised, so that writing publishers doesn't feel like 2013.
Features
- Supports both channels with publisher confirms and regular channels
- Decorates the Fastify Instance with
amqp
object exposing RabbitMQconnection
,channel
andconfirmChannel
following amqplib-as-promised API - Has 100% test coverage
Installation
npm install fastify-amqp-async
The underlying amqplib-as-promised
library exposes some objects of amqplib
native types such as Message
. In order to allow for proper typechecks of such objects, it is recommended to install @types/amqplib
as a development dependency:
npm install --save-dev @types/amqplib
Usage
const fastify = require('fastify');
const fastifyAmqpAsync = require('fastify-amqp-async');
const app = fastify();
const options = {
connectionString: "amqp://user:password@localhost:5672",
useConfirmChannel: false, // true by default
useRegularChannel: true, // false by default
}
app.register(fastifyAmqpAsync, options);
app.get('/produce', async function (req, res) {
const channel = this.amqp.channel;
await channel.assertQueue('queuename', { durable: true });
await channel.sendToQueue('queuename', Buffer.from("Sample message"));
res.send("done");
});
You can find additional usage examples in the examples
folder.
Reference
The config object passed as a second parameter passed to register()
is optional (since all 4 of its keys have default values) has the following schema:
interface FastifyAmqpAsyncOptions {
/**
* AMQP connection string
* @default 'amqp://guest:guest@localhost:5672'
*/
connectionString?: string;
/**
* Spawn a confirm channel (awaiting publisher confirmations) exposed via FastifyInstance.amqp.confirmChannel
* @default true
*/
useConfirmChannel?: boolean;
/**
* Spawn a regular channel (fire-and-forget) exposed via FastifyInstance.amqp.channel
* @default false
*/
useRegularChannel?: boolean;
/**
* Ignore the default onClose handler which closes the connection
* If set to true, you have to manage closing the connection yourself
* (i.e. after waiting for all in-flight messages to be delivered)
* @default false
*/
ignoreOnClose?: boolean;
}
Upon being registered, fastify-amqp-async decorates the FastifyInstance with amqp
exposing the following keys:
connection
- the underlying amqplib-as-promised connection object (API reference)channel
- a single amqplib-as-promised fire-and-forget channel object (API reference). Bare in mind that it will be undefined by default unlessuseRegularChannel
is set totrue
in the config object.confirmChannel
- a single amqplib-as-promised channel with publisher confirms (API reference). Will be undefined ifuseConfirmChannel
is set tofalse
in the config object.