# silly-pubsub

> Simple trivial message subscription and publish utility

Latest version **1.0.4** (published 2025-06-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install silly-pubsub
pnpm add silly-pubsub
yarn add silly-pubsub
bun add silly-pubsub
```

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2025-06-05 |
| First published | 2018-05-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | doomFalc |
| Maintainers | doomfalc |
| Keywords | pubsub |

## Links

- npm: https://www.npmjs.com/package/silly-pubsub
- Repository: https://github.com/doomfalc/silly-pubsub
- Homepage: https://github.com/doomfalc/silly-pubsub#readme
- Issues: https://github.com/doomfalc/silly-pubsub/issues
- npm.io page: https://npm.io/package/silly-pubsub

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.0.4 (latest) — 2025-06-05
- 1.0.3 — 2018-06-02
- 1.0.2 — 2018-05-14
- 1.0.1 — 2018-05-14
- 1.0.0 — 2018-05-12

## README

# Silly Pub Sub

Simple trivial message subscription and publish utility

## Use

### Create a new MessageHandler

```js
const PubSub = require("silly-pubsub");
const pubSub = new PubSub();
```

### Subscribe to an event

```js
pubSub.subscribe("some-event-name", (...args) => {
    // do something
});
```

### Cancel/Terminate an event subscription

```js
// subscribe returns an object that allows event termination
const event = pubSub.subscribe("some-event-name", (...args) => {
    // do something
});
// Terminate
event.done();
```

### Trigger an event

```js
pubSub.publish("some-event-name");
// alternatively, pass any desired argument to access them in the subscribed functions.
pubSub.publish("some-event-name", { whatever: true });
```


## Full example

```js
const PubSub = require("silly-pubsub");

class Vendor {
    constructor(pubSub) {
        this.pubSub = pubSub;
    }
    restock() {
        this.say("We just restocked some SNES, hurray!");
        this.pubSub.publish("snes", "SNES classics are back in stock!");
    }
    say(message) {
        console.log("Vendor -", message);
    }
}

class Customer {
    constructor() {
        this.purchaseEvent = null;
    }
    hopeForSnes(pubSub) {
        this.purchaseEvent = pubSub.subscribe("snes", content => {
            this.say("Vendor says:", content);
            this.say("Place order!!!");
        });
    }
    say(...message) {
        console.log("Customer -", ...message);
    }
    buySnes() {
        this.say("Alright, I'm done.");
        // Now that we've bought a SNES, we don't want to get vendor notifications anymore.
        if (this.purchaseEvent) {
            this.purchaseEvent.done();
        }
    }
}

const pubSub = new PubSub();
const vendor = new Vendor(pubSub);
const customer = new Customer();

customer.hopeForSnes(pubSub);
vendor.restock();
customer.buySnes();
// Here, we shouldn't get any more notification.
vendor.restock();
```

**Output**

```
Vendor - We just restocked some SNES, hurray!
Customer - Vendor says: SNES classics are back in stock!
Customer - Place order!!!
Customer - Alright, I'm done.
Vendor - We just restocked some SNES, hurray!
// Customer doesn't receive a notification anymore.
```

---
_Source: https://npm.io/package/silly-pubsub · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
