
Security News
The Hidden Blast Radius of the Axios Compromise
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.
Dependency free JavaScript pubsub implementation with wildcards, inheritance and multisubscriptions.
Working smoothly both on frontend and backend side.
Read documentation, check tests file, be inspired and feel free to use it and/or contribute.
npm install pubsub.jsbower install pubsub-advancedDefault pubsub.js configuration:
separator : '/' // defined namespace separator
context // has dynamic value - when not defined it will be always reference to used callback in subscribe method
recurrent : false // defines inheritance of publish event
async : false // if true - publish events will be asynchronous
log : false // set to true will log unsubscribed namespaces to which You publish event
Using pubsub inside node.js
var pubsub = require('pubsub.js');
pubsub.subscribe('hello/world', function(text) {
console.log(text);
});
pubsub.publish('hello/world', ['my text']);
//subscribe to 'hello/world' namespace
pubsub.subscribe('hello/world', function() {
console.log('hello world!');
});
//publish event on 'hello/world' namespace
pubsub.publish('hello/world');
//prints "hello world" inside console
//subscribe to 'hello/world' namespace
pubsub.subscribe('hello/world', function(data) {
console.log(data);
});
//publish event on 'hello/world' namespace
pubsub.publish('hello/world', ['hello!']); // second parameter is an array of arguments
//prints "hello!" inside console
//subscribe to 'hello/world' namespace
var subscription = pubsub.subscribe('hello/world', function() {
console.log('hello world!');
});
//publish event on 'hello/world' namespace
pubsub.publish('hello/world');
//prints "hello world" inside console
//unsubscribe
pubsub.unsubscribe(subscription);
//publish event on 'hello/world' namespace
pubsub.publish('hello/world');
//nothing happen - we've previously unsubscribed that subscription
Browser
Before pubsub script loader - make global variable named "pubsub" with your default configuration
pubsub = {
separator : '.'
context : referenceToContext
}
After pubsub load - use it with your configuration, pubsub.js will replace that previous "pubsub" global variable with its own instance
//subscribe to 'hello.world' namespace
var subscription = pubsub.subscribe('hello.world', function() {
console.log('hello world!');
});
//publish event on 'hello.world' namespace
pubsub.publish('hello.world');
//prints "hello world" inside console
//unsubscribe
pubsub.unsubscribe(subscription);
//publish event on 'hello.world' namespace
pubsub.publish('hello.world');
//nothing happen - we've previously unsubscribed that subscription
Node.js
Before pubsub require execution - set global.pubsubConfig variable
global.pubsubConfig = {
separator : '.'
}
After pubsub load, it'll have your configuration as in browser example
//subscribe to 'hello' namespace
var subscription = pubsub.subscribe('hello', function() {
console.log('hello world!');
});
//publish event on 'hello/world' namespace
pubsub.publish('hello/world', [], {
recurrent : true
});
//prints "hello world" inside console
//first event goes to "hello" namespace
//then it tries to execute on "hello/world" but nothing is listening on it
var iterator = 0;
var data = null;
pubsub.subscribeOnce('hello/world', function(param) {
data = param;
iterator++;
});
pubsub.publish('hello/world', ['hello']);
pubsub.publish('hello/world', ['world']);
console.log(iterator); //1
console.log(data); //'hello'
var number = 0;
//subscribe to "hello/world" namespace
pubsub.subscribe('hello/world', function() {
number++;
});
//subscribe to "hello/earth" namespace
pubsub.subscribe('hello/earth', function() {
number++;
});
//subscribe to "hello/galaxy" namespace
pubsub.subscribe('hello/galaxy', function() {
number++;
});
//subscribe to "hello/world/inner" namespace
pubsub.subscribe('hello/world/inner', function() {
number++;
});
pubsub.publish('hello/*');
//hello/* executes:
// hello/world, hello/earth, hello/galaxy
// namespace, hello/world/inner is not executed
//
// "*" goes only one namespace deeper
console.log(number); //3
var number = 0;
var subscription = pubsub.subscribe('hello/*/world', function() {
number += 1;
});
pubsub.publish('hello'); // won't handle
pubsub.publish('hello/my'); // won't handle
pubsub.publish('hello/great/galaxy'); // won't handle
pubsub.publish('hello/my/world'); // handles
pubsub.publish('hello/huge/world'); // handles
pubsub.publish('hello/great/world'); // handles
console.log(number); // 3
many namespaces, one callback
var number = 0;
var subscription = pubsub.subscribe(['hello/world', 'goodbye/world'], function() {
number++;
});
pubsub.publish('hello/world');
console.log(number); //1
pubsub.publish('goodbye/world');
console.log(number); //2
pubsub.unsubscribe(subscription);
pubsub.publish('hello/world');
console.log(number); //2
pubsub.publish('goodbye/world');
console.log(number); //2
one namespace, many callbacks
var number1 = 0;
var number2 = 0;
var subscription = pubsub.subscribe('hello/world', [function() {
number1++;
}, function() {
number2 += 2;
}]);
pubsub.publish('hello/world');
console.log(number1 + ',' + number2); //1,2
pubsub.unsubscribe(subscription);
pubsub.publish('hello/world');
console.log(number1 + ',' + number2); //2,4
many namespaces, many callbacks
var number1 = 0;
var number2 = 0;
var subscription = pubsub.subscribe(['hello/world', 'goodbye/world'], [function() {
number1++;
}, function() {
number2 += 2;
}]);
pubsub.publish('hello/world');
console.log(number1 + ',' + number2); //1,2
pubsub.publish('goodbye/world');
console.log(number1 + ',' + number2); //2,4
pubsub.unsubscribe(subscription);
pubsub.publish('hello/world');
console.log(number1 + ',' + number2); //2,4
pubsub.publish('goodbye/world');
console.log(number1 + ',' + number2); //2,4
###making new instances with own namespaces scope
var number1 = 0;
var number2 = 0;
var privatePubsub = pubsub.newInstance();
pubsub.subscribe('hello/world', function() {
number1++;
});
privatePubsub.subscribe('hello/world', function() {
number2++;
});
pubsub.publish('hello/world');
console.log(number1 + ',' + number2); //1,0
privatePubsub.publish('hello/world');
console.log(number1 + ',' + number2); //1,1
making new instances with own context
var contextArgument = ["object"];
var privatePubsub = pubsub.newInstance({
context : contextArgument
});
privatePubsub.subscribe('hello/context', function() {
var that = this;
console.log(that === contextArgument); //true
});
privatePubsub.publish('hello/context');
###Using pubsub asynchronously
var number1 = 0;
var asyncPubsub = pubsub.newInstance({
async : true
});
asyncPubsub.subscribeOnce('hello/world', function() {
number1++;
console.log(number1); //2
});
asyncPubsub.publish('hello/world'); // asynchronous call to 'hello/world'
number1++;
console.log(number1); //1
###Using context param in subscribe method
var contextArgument = ["object"];
var privatePubsub = pubsub.newInstance();
function callbackFirst() {
var that = this;
console.log(that === callbackFirst); // true
}
function callbackSecond() {
var that = this;
console.log(that === contextArgument); // true
}
var privateSubscribtion1 = privatePubsub.subscribe('hello/context', callbackFirst);
var privateSubscribtion2 = privatePubsub.subscribe('hello/that', callbackSecond, {
context : contextArgument
});
MIT
FAQs
Vanilla JS Pubsub implementation with wildcards and inheritance
We found that pubsub.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
The Axios compromise shows how time-dependent dependency resolution makes exposure harder to detect and contain.

Research
A supply chain attack on Axios introduced a malicious dependency, plain-crypto-js@4.2.1, published minutes earlier and absent from the project’s GitHub releases.

Research
Malicious versions of the Telnyx Python SDK on PyPI delivered credential-stealing malware via a multi-stage supply chain attack.