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

sugarmq

v1.1.0

Published

A sweet syntax wrapper around amqplib

Downloads

13

Readme

SugarMQ

Build Status Coverage Status semantic-release npm version npm downloads

An opinionated declarative wrapper around the node amqplib. We use this library to make it simpler for us to build event driven microservices that use RabbitMQ as a message broker.

Installation

npm install sugarmq --save
# or
yarn add sugarmq

Road Map

This library is still under active development, we encourage you to try it and give us feedback in the issues. Below are some of the the things we intend to work on over the next couple of weeks;

  • [x] Topic Producer and Consumer
  • [ ] Scheduled Producers (publish to a delayed messages exchange)
  • [ ] Direct Queue Producer and Consumer
  • [ ] Create Docker image for RabbitMQ bundled with plugins needed for library.

Usage

Topic Pub/Sub

RabbitMQ allows multiple programs to receive messages about a topic on an exchange. It also allows multiple programs to publish messages about any topic to an exchange. In this section, we'll see how to consume and publish different topics to an exchange.

Creating a topic consumer

// consume.js
const {TopicConsumer} = require('sugarmq');

TopicConsumer.create()
  .setExchange('x')
  .setQueue('q')
  .subscribe('lazy.#', (topic, message) => {
    // Do interesting stuff to lazy.# messages here
    // ...
    console.log(topic, message);
  }) 
  .subscribe('*.*.rabbit', (topic, message) => {
    // Do interesting stuff to *.*.rabbit messages here
    // ...
    console.log(topic, message);
  })
  .start('amqp://localhost');

The above code snippet, does five things;

  • Ensures a topic exchange named "x" exists on our RabbitMQ exchange. It doesn't it will be created. If it does but does not match your declaration, an exception will be thrown and our code will fail.
  • Ensures a queue named "q" exists on the instance. Again, if it doesn't it will be created, and if it exists with a different declaration, our code fails with an "blah blah" exception.
  • Subscribes to all messages sent to exchange x whose topic starts with lazy.
  • Subscribes to all messages sent to exchange x whose topic matches the expression *.*.rabbit
  • Starts the topic consumer. In the background, this establishes an amqp connection, asserts the exchange and queue, and binds topics to queue. When a message is received from the queue for a given topic, the respective callback is called. It is important to note that only one callback is invoked even if more than one match the topic. It is therefore best to ensure you subscribe to mutually exclusive topic globs.

To run this snippet, copy and paste the code to file named "consume.js", and run it as a command;

node consume.js

To see logs from the library;

DEBUG=sugarmq node consume.js

Creating a topic publisher

const {TopicProducer} = require('sugarmq');

TopicProducer.create()
  .setExchange('x')
  .start('amqp://localhost')
  .then(producer => {
    producer.publish("quick.orange.rabbit", "the quick orange rabbit");
    setTimeout(() => producer.stop(), 500);
  });

In the above snippet, our script;

  • Declares an exchange x
  • Starts the producer. In the background this establishes connection, asserts the exchange and returns our producer ready to publish messages). This is an "async" method, so we have to "await" the producer.
  • Publishes to the quick.orange.rabbit topic on exchange x and stops the producer (closes connection) after half a second.

To run this snippet, copy and paste the code to file named 'produce.js', and run it as a command;

node produce.js

To see logs from the library;

DEBUG=sugarmq node produce.js