PubSubThen is a synchronous publish/subscribe node.js module.
PubSubThen was developed for the purpose of complete separation of modules in node.js.
PubSubThen supports a synchronous topic publication.
There are several ways of getting PubSubThen
- Download a tagged version from GitHub
- Install via npm (
npm install pub-sub-then
)
// Require
var PubSubThen = require('../lib/PubSubThen');
// Add a subscription to topic
PubSubThen.subscribe('my-message', function(arg1, arg2, next) {
// Do something with arg1
arg1.push('new value');
// Do something with arg2
arg2.push('new value 2');
next();
});
// Publish the topic
var list1 = [];
var list2 = [];
PubSubThen.publish('my-message', list1, list2).then(
function() {
try {
console.log(list1);
console.log(list2);
} catch(e) {
}
},
function(err) {
throw err;
}
);
// Add a subscription to topic
var subscription = PubSubThen.subscribe('my-message', function(args, next) {
// Do something
next();
});
PubSubThen.unsubscribe('my-message', subscription);
// Add a subscription to topic
PubSubThen.subscribe('my-message', function(args, next) {
// FUNC 1
next();
});
PubSubThen.subscribe('my-message', function(args, next) {
// FUNC 2
next();
});
PubSubThen.removeAllSubscription('my-message');