PubSub/A is an interface for libraries implementing the Publish/Subscribe pattern (also often referred to as Observer pattern). The aim of PubSub/A is to provide a unified, asynchronous common API for pubsub (similiar to the Promise/A proposal that defined a unified API for promises).
PubSub/A uses the so-called topic-based Publish/Subscribe with the additional concept of channels for grouping together topics.
A reference implementation written in TypeScript (but usable from plain JavaScript) is available here.
const pubsub = new PubSubImplementation();
// channels are initialized asynchronously, provide a way to namespace topics
pubsub.channel('mychannel').then(channel => {
// subscribe
channel.subscribe('myTopic', arg => {
console.log('I received a notification: ' + arg.message);
})M
// publish - you can pass any custom object as argument
channel.publish('myTopic', { message: 'Hello world!' });
});
The specification is included in the SPECIFICIATION.md file. The interface definitions in this project are written as TypeScript interfaces to provide a statically typed API for better understanding. As this project only includes interfaces and not actual implementations, the resulting output from the TypeScript compiler is an empty JavaScript file.
- pubsub-micro - A reference implementation written in TypeScript but usable from any JavaScript project
An official test suite is available to test your implementation for validity.
MIT.