0.1.1 • Published 3 years ago

usagi v0.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Pygmy Rabbit

Rabbit made simple :rocket:

API Documentation

pygmy-rabbit

TL;DR

Publisher

const Publisher = require("pygmy-rabbit/publisher")

const pub = new Publisher(
  "amqp://user:password@rabbitmq",
  "logger"
)

await pub.connect()

pub.emit("logs.pub.critical", { date: new Date(), msg: "Something went wrong" })
pub.emit("logs.pub.warn",     { date: new Date(), msg: "Well that could have been worse" })
pub.emit("logs.other.notice", { date: new Date(), msg: "Move along" })

Subscriber

const Subscriber = require("pygmy-rabbit/subscriber")

const sub = new Subscriber(
  "amqp://user:password@rabbitmq",
  "logger"
)

await sub.connect()

sub.on("#.critical", console.error)
// { date: "2010-11-12T13:14:15.016Z", msg: "Something went wrong" }

sub.on("logs.pub.warn", console.log)
// { date: "2010-11-12T13:14:15.016Z", msg: "Well thar could have been worse" }

sub.on("*.other.*", console.log)
// { date: "2010-11-12T13:14:15.016Z", msg: "Move along" }

Usage

Client

The client is the underlying structure that connects either the subscriber or the publisher to the rabbitmq server

If your service has both a subscriber and a publisher, you should use it as so:

const { Client, Subscriber: Sub, Publisher: Pub } = require("pygmy-rabbit")

const client = new Client("amqp://user:password@rabbitmq")
const sub = new Sub(client, "exchange")
sub.connect() // required to assert channel and queue
const pub = new Pub(client, "exchange")
pub.connect() // required to assert channel

Publisher

The publisher allows you to send data to the exchange or queue

:warning: As of today, only the exchange creation works

const Publisher = require("pygmy-rabbit/publisher")
const pub = new Publisher(
  "amqp://user:password@server:port", // the connection string to the server
  {
    name: "foo",                      // the name of the exchange
    durable: true,                    // should the queue survive a broker restart
    type: "topic|fanout|direct|header" // See the documentation (topic by default)
  }
)
// Note, if you want a durable topic exchange, you can just
const pub = new Publisher("amqp://user:password@server:port", "name")

Subscriber

The subscriber handles receiving messages

:warning: not must is handled atm

const Subscriber = require("pygmy-rabbit/subscriber")
const pub = new Subscriber(
  "amqp://user:password@server:port", // the connection string to the server
  {
    name: "exchange",                 // the name of the exchange
    durable: true,                    // should the queue survive a broker restart
    type: "topic|fanout|direct|header", // See the documentation (topic by default)
    prefetch: 1                       // Whether to prefetch messages and how much (null for no prefetch)
  },
  {
    name: "",                         // let the server decide how to name the queue
    exclusive: true,                  // whether to delete the queue once this consumer ends
    persistent: false                 // write the message persistently to the drive
  }
)
// Note, if you want a durable topic exchange to exclusive queues, you can just
const pub = new Subscriber("amqp://user:password@server:port", "exchange")
// If however you want multiple subscribers to the same topic, you should use a named queue
const pub = new Subscriber("amqp://user:password@server:port", "exchange", "queue")
// which is equivalent to
const pub = new Subscriber(
  "amqp://user:password@server:port", // the connection string to the server
  "exchange",                         // the name of the exchange hasn't changed
  {
    name: "queue",                    // let the server decide how to name the queue
    exclusive: FALSE,                 // a named queue shouldn't be exclusive
    persistent: TRUE                  // but may be persistent
  }
)