/awesome-pubsub-js

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

Primary LanguageTypeScript

awesome-pubsub-js

NPM JavaScript Style Guide

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

Install

npm

$ npm install awesome-pubsub-js

yarn

$ yarn add awesome-pubsub-js

A quick example

Do you want to know more? Go to the Documentation section

JavaScript:
import PubSub from "awesome-pubsub-js";

const pubSub = 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')
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

Roadmap:

  • subscribe and publish method
  • unsubscribe method
  • getAllSubscribers method
  • Wildcard support
  • Logger