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

@rlanz/bus

v0.4.0

Published

A simple and lean driver-based service bus implementation

Downloads

36

Readme

typescript-image gh-workflow-image npm-image npm-download-image license-image

@rlanz/bus is a service bus implementation for Node.js. It is designed to be simple and easy to use.

Currently, it supports the following transports:

Table of Contents

Installation

npm install @rlanz/bus

Usage

The module exposes a manager that can be used to register buses.

import { BusManager } from '@rlanz/bus'
import { redis } from '@rlanz/bus/transports/redis'
import { memory } from '@rlanz/bus/transports/memory'

const manager = new BusManager({
  default: 'main',
  transports: {
    main: {
      transport: memory(),
    },
    redis: {
      transport: redis({
        host: 'localhost',
        port: 6379,
      }),
    }
  }
})

Once the manager is created, you can subscribe to channels and publish messages.

manager.subscribe('channel', (message) => {
  console.log('Received message', message)
})

manager.publish('channel', 'Hello world')

By default, the bus will use the default transport. You can specify different transport by using the use method.

manager.use('redis').publish('channel', 'Hello world')

Without the manager

If you don't need multiple buses, you can create a single bus directly by importing the transports and the Bus class.

import { Bus } from '@rlanz/bus'
import { RedisTransport } from '@rlanz/bus/transports/redis'

const transport = new RedisTransport({
  host: 'localhost',
  port: 6379,
})

const bus = new Bus(transport, {
  retryQueue: {
    retryInterval: '100ms'
  }
})

Retry Queue

The bus also supports a retry queue. When a message fails to be published, it will be moved to the retry queue.

For example, your Redis server is down.

const manager = new BusManager({
  default: 'main',
  transports: {
    main: {
      transport: redis({
        host: 'localhost',
        port: 6379,
      }),
      retryQueue: {
        retryInterval: '100ms'
      }
    },
  }
})

manager.use('redis').publish('channel', 'Hello World')

The message will be moved to the retry queue and will be retried every 100ms.

You have multiple options to configure the retry queue.

export interface RetryQueueOptions {
  // Enable the retry queue (default: true)
  enabled?: boolean
  
  // Defines if we allow duplicates messages in the retry queue (default: true)
  removeDuplicates?: boolean
  
  // The maximum size of the retry queue (default: null)
  maxSize?: number | null
  
  // The interval between each retry (default: false)
  retryInterval?: Duration | false
}

Test helpers

The module also provides some test helpers to make it easier to test the code that relies on the bus. First, you can use the MemoryTransport to create a bus that uses an in-memory transport.

You can also use the ChaosTransport to simulate a transport that fails randomly, in order to test the resilience of your code.

import { Bus } from '@rlanz/bus'
import { ChaosTransport } from '@rlanz/bus/test_helpers'

const buggyTransport = new ChaosTransport(new MemoryTransport())
const bus = new Bus(buggyTransport)

/**
 * Now, every time you will try to publish a message, the transport 
 * will throw an error.
 */
buggyTransport.alwaysThrow()