Skip to content
forked from boringnode/bus

A simple and lean service bus implementation for Node.js

License

Notifications You must be signed in to change notification settings

JuliensForks/bus

 
 

Repository files navigation

@rlanz/bus

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 drivers:

👉 Memory: A simple in-memory driver for testing purposes.
👉 Redis: A Redis driver for production usage.

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/drivers/redis"
import { memory } from "@rlanz/bus/drivers/memory"

const manager = new BusManager({
  default: 'main',
  transports: {
    main: {
      driver: memory(),
    },
    redis: {
      driver: 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')

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: {
      driver: 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
}

About

A simple and lean service bus implementation for Node.js

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%