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

@zijin-m/rascal

v1.1.0

Published

wrapper for rascal supporting default publication, subscription handlers, auto republish message when confirm failed and auto ackOrNack when no error emit form your handler

Downloads

6

Readme

@zijin-m/Rascal

@zijin-m/Rascal wrapper for rascal to support default publication, subscription handlers, auto republish message when confirm failed, auto ackOrNack when no error emit from your handler. only support es6+;

NPM version NPM downloads Build Status Code Style Dependency Status devDependencies Status

About

Rascal is a rich pub/sub wrapper for the excellent amqplib. One of the best things about Rascal is that make amqplib easier.

This project want to add more default features for rascal that:

  • default broker error handler
  • default publication event handler for success, return and error
  • republish message when confirm error from rabbitMQ
  • default subscription event handler for message, error and so on
  • auto ackOrNack message when no error throw form handler
  • ts support

Examples

Config

With a Rascal config, read more

module.exports = {
    vhosts: {
        "/": {
            connection: {
                protocol: "amqp",
                hostname: "127.0.0.1",
                port: 5672,
                user: "guest",
                password: "guest",
            },
            queues: {
                "order.save.service_b": {
                    assert: true,
                    options:{
                        arguments: {
                            "x-dead-letter-exchange": "dead_letters",
                            "x-dead-letter-routing-key": "order.save",
                        }
                    }
                },
                "dead_letters.order.save.service_b": {
                    assert: true
                }
            },
            exchanges: {
                order: {
                    type: "direct",
                    assert: true
                },
                dead_letters: {
                    type: "direct",
                    assert: true,
                }
            },
            bindings: {
                "order.save.service_b": {
                    source: "order",
                    bindingKey: "save",
                    destination: "order.save.service_b",
                    destinationType: "queue"
                },
                "dead_letters.order.save.service_b": {
                    source: "dead_letters",
                    bindingKey: "order.save",
                    destination: "dead_letters.order.save.service_b",
                    destinationType: "queue"
                },
            },
            publications: {
                "order.save": {
                    vhost: "/",
                    exchange: "order",
                    routingKey: "save"
                },
            },
            subscriptions: {
                "order.save": {
                    queue: "order.save.service_b",
                    prefetch: 1,
                    vhost: "/",
                    recovery: "deferred_retry",
                    redeliveries: {
                        limit: 10,
                        counter: "shared"
                    }
                },
            }
        }
    },
    recovery: {
        deferred_retry: [
            {
                strategy: "nack",
                requeue: true,
                defer: 10 * 1000
            }
        ]
    },
    redeliveries: {
        counters: {
            shared: {
                type: "inMemory",
            }
        }
    }
};

Publish

import { Broker, Consumer } from "@zijin-m/rascal";
import config from "./config";

const broker = await Broker.create(config)
const publication = await broker.publish('order.save', 'some message') 

when you publish message, Publication will use a failedQueue that save all failed confirm message, to retry failed messages when connection recovery event emit or interval timer success publish message that is head of failedQueue .

Note

  • message push to failedQueue only rabbitMQ return error confirm event, when return return confirm event, Publication will nack message, leading to message loss if you have not configured a dead letter exchange/queue.

you can also add your event handlers by listen broker.publish returned eventemitter :

publication.on('error', console.error);
publication.on('return', console.log);
publication.on('success', console.log);

Subscribe

import { Broker, Consumer, Message } from "@zijin-m/rascal";
import config from "./config";

class OrderSaveCousumer extends Consumer {

    public readonly name = "order.save"; // set subscribe name or use new OrderSaveCousumer("order.save")

    async onMessage(content: any, message: Message) {
        // your code
        // no need to call ackOrNack if your code success, Consumer will do this for you
    }
}

const broker = await Broker.create(config)
await broker.addConsumer(new OrderSaveCousumer())

auto ack

you can write your message handler code in onMessage method, here are a few things that can happen:

onMessage call completed normally

Consumer will call ackOrNack() to ack message.

onMessage call throw error

Consumer will call ackOrNack(error) to not ack message, leading to message loss if you have not configured a dead letter exchange/queue.

onMessage call throw error and you config redeliveries

if you do want to retry some times when your code throw error for network failed and other reason that can success handle this message after retry, you can config redeliveries in config file that named config.json or config.ts like examples,

subscriptions: {
    "order.save": {
        queue: "order.save.service_b",
        prefetch: 1,
        vhost: "/",
        recovery: "deferred_retry", // use recovery
        redeliveries: {
            limit: 10, // max retry time
            counter: "shared"
        }
    },
},
// define recovery
recovery: {
  deferred_retry: [
      {
          strategy: "nack", // nack message
          requeue: true, // tell rabbitMQ to requeue requeue message
          defer: 10 * 1000 // defer 10 * 1000 ms to nack message for wait other service or network to restore 。
      }
  ]
},
redeliveries: {
  counters: {
      shared: {
          type: "inMemory", // save timer in memory
      }
  }
}

after config, Consumer will use recovery deferred_retry to retry 10 time, each time defer 10 * 1000 ms to wait network or other make message failed reason recover. and if all 10 time failed, Consumer will ackOrNack(err) by listen redeliveries_exceeded that emitted by rascal redeliveries_exceeded, leading to message loss if you have not configured a dead letter exchange/queue.

Implementing your own counter

inMemory counter only save timer in memory, it will loss when restart you app, and not so accurate in cluster mode 。If want to protect yourself from redeliveries, you need to implement your own counter backed by something like redis. read more implementing-your-own-counter.

custom ack message

if you want to custom message by call ackOrNack your self, you can do like :

class OrderSaveConsumer extends Consumer {
    async onMessage(content: any, message: Message, ackOrNack: AckOrNackFn) {
        // your code
        // see https://github.com/guidesmiths/rascal#message-acknowledgement-and-recovery-strategies
        ackOrNack(err, { strategy: 'republish', defer: 1000 });
    }
}

when you manual call ackOrNack , Consumer will not call it more.