Skip to content

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

Notifications You must be signed in to change notification settings

a-bugaj/awesome-pubsub-js

Repository files navigation

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: "[email protected]" }
    // ... your logic
});

pubSub.publish("event.example", { name: "John", email: "[email protected]" });

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

About

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

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published