@kettek/pubsub

PubSub library with glob matching support.

Usage no npm install needed!

<script type="module">
  import kettekPubsub from 'https://cdn.skypack.dev/@kettek/pubsub';
</script>

README

@kettek/pubsub

This library provides a relatively simple PubSub implementation that supports glob matching for both subscriptions and for publishing.

Installation

npm i @kettek/pubsub

Documentation

See the TypeDoc generated documentation.

Usage

import { Publisher } from '@kettek/pubsub'

const p = new Publisher()

let s = p.subscribe('topic.golang', async ({ message }) => {
    console.log(message)
})

p.publish('topic.golang', 'Hello from topic.golang!')
// Hello from topic.golang!

p.unsubscribe(s)

Wildcards can be used for subscriptions:

let s = p.subscribe('topic.*', async ({ message }) => {
    console.log(message)
})

p.publish('topic.golang', 'Hello from topic.golang!')
p.publish('topic.rust', 'Hello from topic.rust!')
// Hello from topic.golang!
// Hello from topic.rust!

Wildcards can also be used for publishing:

let s = p.subscribe('topic.golang', async ({ message }) => {
    console.log(message)
})
let s2 = p.subscribe('topic.rust', async ({ message }) => {
    console.log(message)
})

p.publish('topic.*', 'Hello from topic.*!')
// Hello from topic.*!
// Hello from topic.*!

Await can be used to wait for all subscribers to be published to:

let s = p.subscribe('topic.golang', async ({ message }) => {
    console.log(message)
})

await p.publish('topic.golang', 'Hello from topic.golang!')

A single subscriber can be used for multiple topic subscriptions:

let s = p.subscribe('topic.golang', async ({ message }) => {
    console.log(message)
})
p.subscribe('topic.rust', s)

p.publish('topic.golang', 'Hello from topic.golang!')
p.publish('topic.rust', 'Hello from topic.rust!')
// Hello from topic.golang!
// Hello from topic.rust!

A subscriber can be unsubscribed from a topic:

p.unsubscribe('topic.rust', s)
// s is still subscribed to topic.golang

A subscriber can also be unsubscribed from all subscribed topics:

p.unsubscribe(s)
// s is no longer subscribed to any topics.

A handler/callback can be used instead of a subscriber:

let h = async ({message}) => {
    console.log('handled', message)
}

p.subscribe('topic.golang', h)
p.subscribe('topic.rust', h)

p.publish('topic.golang', 'Hello from topic.golang!')
p.publish('topic.rust', 'Hello from topic.rust!')
// Hello from topic.golang!
// Hello from topic.rust!

p.unsubscribe('topic.rust', h)
// h is now only subscribed to topic.golang.
p.unsubscribe(h)
// h is now unsubscribed from all subscriptions.