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

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

461

Readme

Fastify AMQP plugin with a modern, promise-based API

CI Workflow NPM version NPM downloads Known Vulnerabilities

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 RabbitMQ connection, channel and confirmChannel 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 unless useRegularChannel is set to true in the config object.
  • confirmChannel - a single amqplib-as-promised channel with publisher confirms (API reference). Will be undefined if useConfirmChannel is set to false in the config object.