@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.
npm install @rlanz/bus
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')
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
}