@qphi/publisher-subscriber
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

Publisher-Subscriber

Let your vanilla components work together by publishing or subscribing notifications!

Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Documentation
  4. License
  5. Contact

About The Project

@qphi/publisher-subscriber is a module built with Typescript as UMD module. It aims to implement most event / messaging patterns in your js or ts projects.

This module provides two main interfaces:PublisherInterface and SubscriberInterface that describe how your vanilla object could share notification in order be reactive and work together.

👉 Note that it is not an academical pubsub pattern.

Getting Started 🚀

Prerequisites

  • Adding the module to your project
    npm install @qphi/publisher-subscriber --save

That's it! Now you can start to play with the notification system!

Send your first notification

import {Publisher, Subscriber} from "@qphi/publisher-subscriber";

const publisher = new Publisher('publisher-id');
const subscriber = new Subscriber('subscriber-id');

subscriber.subscribe(publisher, 'notification-string-example', () => {
    console.log("Hello world! I'm a happy handler!");
});

publisher.publish('notification-string-example');
// => "Hello world! I'm a happy handler!"

// clear your component properly
publisher.destroy();
subscriber.destroy();

Send parameters on publish

import {Publisher, Subscriber} from "@qphi/publisher-subscriber";

const publisher = new Publisher('Paul');
const subscriber = new Subscriber('bar');

subscriber.subscribe(publisher, 'hi', name => {
    console.log(`Hi, my name is ${name}! Nice to meet you!`);
});

publisher.publish('hi', publisher.getId());
// => "Hi, my name is Paul! Nice to meet you!"

// clear your component properly
publisher.destroy();
subscriber.destroy();

Combine Publisher and Subscriber roles

An instance of PublisherSubscriber implements PublisherInterface and SubscriberInterface. That means it can subscribe to some notifications and also publish.

This kind of instance is helpful when you have to manage several components or implement a workflow.

import { PublisherSubscriber } from "@qphi/publisher-subscriber";

const worker = new PublisherSubscriber('worker');
const manager = new PublisherSubscriber('manager');

worker.subscribe(manager, 'new-mission-available', jobId => {
   console.log(`${worker.getId()}: "${manager.getId()}" asks somebody to do the job "${jobId}".`);
   // some business logic here
   console.log(`${worker.getId()}: job "${jobId}" is done.`);
   worker.publish('job-done', jobId); 
});

manager.subscribe(worker, 'job-done', jobId => {
    // some business logic here
    console.log(`${manager.getId()}: "${worker.getId()}" notices me that job "${jobId}" was done.`);
});


manager.publish('new-mission-available', 'foo');
// => worker: "manager" asks somebody to do the job "foo".
// => worker: job "foo" id done.
// => manager: "worker" notices me that job "foo" was done.

// clear your component properly
manager.destroy();
worker.destroy();

Documentation

https://qphi.github.io/publisher-subscriber/

Roadmap

See the roadmap section for a full list features already planed.
See the issues section for a full list of proposed features (and known issues).

(back to top)

License

Distributed under the GPL License. See LICENSE.txt for more information.

(back to top)

Contact

Quentin Philippot - qphi-developper@gmail.com

Project Link: https://github.com/qphi/publisher-subscriber

(back to top)

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.2.1
    54
    • latest

Version History

Package Sidebar

Install

npm i @qphi/publisher-subscriber

Weekly Downloads

54

Version

1.2.1

License

GPL-3.0

Unpacked Size

237 kB

Total Files

101

Last publish

Collaborators

  • qphi