@qphi/publisher-subscriber
v1.2.1
Published
A library to implements messages patterns and manage data flow between objects
Downloads
285
Maintainers
Readme
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).
License
Distributed under the GPL License. See LICENSE.txt
for more information.
Contact
Quentin Philippot - [email protected]
Project Link: https://github.com/qphi/publisher-subscriber