1.0.3 • Published 7 years ago
mulchan v1.0.3
mulchan
A library for a pub-sub channel system. Allows subscribers to register on multiple named channels. Channels are named strings which are a stream of some generic data type. Base implementation of a ChannelsPublisher included. Implement your own ISubscriber to consume ChannelsPublisher messages.
Installation
npm install mulchan --save
Usage
Javascript
// 1.
var ChannelsPublisher = require('mulchan').ChannelsPublisher;
const pub = new ChannelsPublisher()
// 2.
//and the lord said let there be subscribers...
var a_sub = {
next: (message) => //do something subscriby
//...
}
// 3.
//specify which channels you want your subscriber subscribing on
pub.subscribe(a_sub, ['one'])
//...
pub.subscribe(b_sub, ['one'])
// multiple channels supported
pub.subscribe(a_sub, ['two'])
// or load multiple at a time
pub.subscribe(a_sub, ['LOG', 'ERROR', 'NOTIFICATIONS', 'EMAIL-TRIGGER'])
// 4.
//after subs configured, let holders of your pub notify subs
pub.publish('Something relevant happened', ['LOG'])
pub.publish({ hash: ..., transaction_id: ..., ...}, ['TRANSACTIONS'])
// 5.
// unsub subscribers when they've existed to finality.
pub.unsubscribe(a_sub) // clears a sub from ALL of its channels
pub.unsubscribe(a_sub, ['subset', 'of', 'channels'])
TypeScript
import { ChannelsPublisher, ISubscriber } from 'mypluralize';
class SimpleLogSubscriber implements ISubscriber<string> {
// ISubscriber<TMessage>, here TMessage = string, ergo message: string
next(message: string): void {
console.log(message)
}
}
let pub = new ChannelsPublisher<string>();
let sub = new SimpleLogSubscriber();
pub.subscribe(sub, ['channel'])
//...
Test
npm run test