# awesome-pubsub-js

> JavaScript implementation of the Publish/Subscribe pattern with TypeScript support.

Latest version **2.0.0** (published 2025-02-07) · ISC license · 0 weekly downloads

## Install

```sh
npm install awesome-pubsub-js
pnpm add awesome-pubsub-js
yarn add awesome-pubsub-js
bun add awesome-pubsub-js
```

## Health

**Score 40/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2025-02-07 |
| First published | 2021-07-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 5 |
| Maintainers | a-bugaj |

## Links

- npm: https://www.npmjs.com/package/awesome-pubsub-js
- Repository: https://github.com/a-bugaj/awesome-pubsub-js
- Homepage: https://github.com/a-bugaj/awesome-pubsub-js#readme
- Issues: https://github.com/a-bugaj/awesome-pubsub-js/issues
- npm.io page: https://npm.io/package/awesome-pubsub-js

## Recent versions

- 2.0.0 (latest) — 2025-02-07
- 1.1.0 — 2021-07-26
- 1.0.0 — 2021-07-24

## README

# awesome-pubsub-js

[![NPM](https://img.shields.io/npm/v/awesome-pubsub-js.svg)](https://www.npmjs.com/package/awesome-pubsub-js) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

JavaScript implementation of the Publish/Subscribe pattern with TypeScript support.

# Install

### npm

```shell
$ npm install awesome-pubsub-js
```

### yarn

```shell
$ yarn add awesome-pubsub-js
```

# A quick example

Do you want to know more? Go to the [Documentation](#API) section

##### JavaScript:

```js
import PubSub from "awesome-pubsub-js";

const pubSub = new PubSub();

pubSub.subscribe("event.example", (data) => {
    // data = { name: "John", email: "john@gmail.com" }
    // ... your logic
});

pubSub.publish("event.example", { name: "John", email: "john@gmail.com" });
```

### API

**List of all available methods:**

Each of the following methods takes one or two arguments

- Subscribe

| Method name       | Payload                                                                                                                                                       | Return value                                                                                                                                            |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| subscribe         | `pubSub.subscribe("eventName", (data) => {});`                                                                                                                | HashKey (string) - is needed for use in the 'unsubscribe' method                                                                                        |
| unsubscribe       | `pubSub.unsubscribe('hashKey')` <br /> hashKey (string) is always returned from the subscribe method                                                          | true - when the event has been successfully unsubscribed <br /> false - when the event does not exists                                                  |
| publish           | `pubSub.publish('eventName', any)` <br /> any = literally anything you want to pass :) <br /> If you don't pass anything, the default value will be undefined | true - when the event has published successfully <br /> false - when the event has not been published (e.g. due to the lack of a registered subscriber) |
| getAllSubscribers | -                                                                                                                                                             | returns the current subscription list                                                                                                                   |

### Roadmap:

- [x] subscribe and publish method
- [x] unsubscribe method
- [x] getAllSubscribers method
- [ ] Wildcard support
- [ ] Logger

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