GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - boringnode/bus: A simple and lean service bus implementation for Node.js

BranchesTags
Open more actions menu

Latest commit

 

History

168 Commits
168 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
bin
bin
 
 
config
config
 
 
src
src
 
 
test_helpers
test_helpers
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.yarnrc.yml
.yarnrc.yml
 
 
LICENSE.md
LICENSE.md
 
 
README.md
README.md
 
 
compose.yml
compose.yml
 
 
eslint.config.js
eslint.config.js
 
 
index.ts
index.ts
 
 
package.json
package.json
 
 
tsconfig.json
tsconfig.json
 
 
tsup.config.ts
tsup.config.ts
 
 
yarn.lock
yarn.lock
 
 

Repository files navigation

@boringnode/bus

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


@boringnode/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:

šŸ‘‰ Memory: A simple in-memory transport for testing purposes.
šŸ‘‰ Redis: A Redis transport for production usage.
šŸ‘‰ Mqtt: A Mqtt transport for production usage.

Table of Contents

  • Installation
  • Usage
  • Retry Queue

Installation

npm install @boringnode/bus

The Redis and MQTT transports rely on optional peer dependencies. Install the client of the transport you use.

npm install ioredis # for the Redis transport
npm install mqtt # for the MQTT transport

If your project has neither ioredis nor mqtt installed and uses skipLibCheck: false, import types from @boringnode/bus/types/core. @boringnode/bus/types/main also re-exports the Redis and MQTT types.

Usage

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

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

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

Once the manager is created, you can subscribe to channels and publish messages. A bus does not receive the messages it publishes itself, so the subscriber and the publisher usually live in different processes, connected through a shared transport like Redis.

// In one process
await manager.use('redis').subscribe('channel', (message) => {
  console.log('Received message', message)
})

// In another process
await manager.use('redis').publish('channel', 'Hello world')

The memory transport only delivers messages to other buses of the same process.

An error thrown or rejected by a handler does not crash the process, and other handlers still run. The bus catches it and logs it when NODE_DEBUG=boringnode:bus is set. Catch errors inside the handler if you need to react to them.

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')
manager.use('mqtt').publish('channel', 'Hello world')

MQTT topic filters

The MQTT transport accepts topic filters with the + and # wildcards, and calls the handlers of every filter matching a received topic. When filters overlap, such as orders/+ and orders/#, some brokers like Mosquitto and EMQX send one copy of the message per matching subscription, and the handlers of these filters are called once per copy. This happens with MQTT 5 too, so avoid overlapping filters when a handler must run once per message.

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 '@boringnode/bus'
import { RedisTransport } from '@boringnode/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('main').publish('channel', 'Hello World')

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

The retry queue only retries publishing. Redis and MQTT pub/sub deliver messages at most once: a successful publish does not mean a subscriber received the message, and subscribers that are disconnected when it is published never get it. If you need guaranteed delivery, use a durable mechanism such as Redis Streams or message acknowledgements in your application.

Redis Cluster does not guarantee message ordering either: it sends a keyless PUBLISH to a random node, so a subscriber can receive messages published one after the other in a different order.

You have multiple options to configure the retry queue.

export interface RetryQueueOptions {
  // Enable the retry queue (default: true)
  enabled?: boolean
  
  // Ignore a message when the same payload is already queued for the same channel (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 '@boringnode/bus'
import { MemoryTransport } from '@boringnode/bus/transports/memory'
import { ChaosTransport } from '@boringnode/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()

About

A simple and lean service bus implementation for Node.js

Topics

busframework-agnosticnodejs

Resources

Readme
MIT license
Activity
Custom properties

Stars

43 stars

Watchers

4 watching

Forks

9 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800Ɨ600. No cookies required.