postbus
v2.0.0
Published
Buffered message bus
Downloads
4
Maintainers
Readme
Postbus
Buffered message bus
Postbus is basic buffered message bus written in TypeScript. It runs in the browser, or on the server using node.js.
Setup
yarn add postbus
or
npm install --save postbus
Usage
Before you start import the library
import Postbus from 'postbus'
Basic usage
// Setup a new bus with no buffer
const bus = new Postbus()
// Data published can be anything
const data = {}
const message = 'Hi'
const number = 1
// Setup a subscriber
bus.subscribe(context => {
console.log(context)
})
// Publish some data
bus.publish(data)
bus.publish(message)
bus.publish(number)
// Cleanup
bus.unsubsribe(subscriber)
Buffered usage
// Setup a new bus with a buffer of 2 items
const bus = new Postbus(2)
// Data published can be anything
const data = {}
const message = 'Hi'
// Publish before you subscribe
bus.publish(data)
bus.publish(message)
// Because this will exceed the buffer size, it will push the first item out
bus.publish(number)
// The subscription will go over all messages in the buffer
bus.subscribe(context => {
console.log(context)
})
// You can still pusblish here too, and the previous subscriber will catch it all
bus.publish(data)
// Cleanup
bus.unsubsribe(subscriber)