JavaScript implementation of the Publish/Subscribe pattern with TypeScript support.
$ npm install awesome-pubsub-js
$ yarn add awesome-pubsub-js
Do you want to know more? Go to the Documentation section
import PubSub from "awesome-pubsub-js";
const pubSub = PubSub();
pubSub.subscribe("event.example", (data) => {
// data = { name: "John", email: "[email protected]" }
// ... your logic
});
pubSub.publish("event.example", { name: "John", email: "[email protected]" });
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') hashKey (string) is always returned from the subscribe method |
true - when the event has been successfully unsubscribed false - when the event does not exists |
publish | pubSub.publish('eventName', any) any = literally anything you want to pass :) If you don't pass anything, the default value will be undefined |
true - when the event has published successfully false - when the event has not been published (e.g. due to the lack of a registered subscriber) |
getAllSubscribers | - | returns the current subscription list |
- subscribe and publish method
- unsubscribe method
- getAllSubscribers method
- Wildcard support
- Logger