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

rmqbroker

v0.2.0

Published

RMQBroker allows you to create a message producer, a consumer and a subscriber each with their own connection to the rabbitmq broker server.

Downloads

1

Readme

RMQBroker

The aim of this module is to abstract away two different way messages are exchanges using queues.

One way is to send messages in real time, and no matter if we lose them if nobody is waiting for them. You can see this like a stream of messages, always flowing.

You can refer to this also as the pub/sub pattern described here

Another, is when we need to store messages that must be read another point in time, and we must ensure that every message produced is correctly delivered without it can be lost during its way to a consumer.

You can refer to this also as the Work Queues described here

So to sum up, we have four service objects:

  • a DirectPubliser: send messages directly to a queue where they are stored waiting to be consumed.
  • a FanoutPubliser: publish messages to an exchange. No queue is binded to this exchange, so if there are no subscriber messages will be lost.
  • a Consumer: bind the queue to the exchange and start listening for incoming messages.
  • a Subscriber: creates and bind an unamed queue to the exchange and start listening for incoming messages. The queue will be auto removed when Subscriber disconnects.

Install

npm i --save rmqbroker

Usage

All the above mentioned service objects are created using RMQBuilder static object's methods. This is the only object exported by the module.

Exchange real time messages

To exchange real time messages we need a FanoutPublisher and a Subscriber.

Publisher

const { RMQBuilder } = require('rmqbroker')

const publisher = RMQBuilder.createFanoutPublisher('amqp://localhost', 'ex-demo-01', { durable: false })

publisher.publishMessage(message)
    .then(() => {
        console.log('message sent!')
        return publisher.close()
    })
    .then(() => {
        console.log('Goodbye')
    })
    .catch(console.error)

Subscriber

const { RMQBuilder } = require('rmqbroker')

const subscriber = RMQBuilder.createSubscriber('amqp://localhost', 'ex-demo-01', { durable: false })

subscriber.on('message', (message) => {
    console.log(`New message: ${message}`)
})

subscriber.subscribe()
    .then(() => {
        console.log('Waiting for incoming messages...')
    })
    .catch(console.error)

Note that we only declare the same exchange name on the publlisher and the subscriber. The last object refers to the exchange options, the durable property is set to false by default.

There can be more than one subscriber per exchange. Each of the subscriber recive a copy of the same message.

Work queue

If we don't care about to loose messages we need a DirectPublisher and a Consumer.

Publisher

const { RMQBuilder } = require('rmqbroker')

const publisher = RMQBuilder.createDirrectPublisher('amqp://localhost', 'ex-demo-02', 'cool-queue', { durable: true }, { durable: true })

publisher.publishMessage(message)
    .then(() => {
        console.log('message sent!')
        return publisher.close()
    })
    .then(() => {
        console.log('Goodbye')
    })
    .catch(console.error)

Consumer

const { RMQBuilder } = require('rmqbroker')

const consumer = RMQBuilder.createConsumer('amqp://localhost', 'ex-demo-02', 'cool-queue', { durable: true }, { durable: true })

consumer.startConsuming((message) => {
    console.log(message)
})
.then(() => {
    console.log('Callback registered to consumer - waiting for messaging')
})
.catch(console.error)

Note that alongside the exchange we must declare a queue name that must be the same between publisher and consumer. Also the durable option for both exchange and consumer is set to true by default.

There can be more than one consumer per queue, in this case messages are distribuited among all consumers in a way called round-robin.

Example

You can find some examples in the example/ directory.