npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

radjs-publish-subscribe

v1.0.0

Published

Message bus implementation via pubsub pattern

Downloads

3

Readme

PubSub

Implementation of the publisher-subscriber pattern for building an application that uses event bus in the application architecture.

It allows not only to deliver messages to registered users through channels, but also manage the delivery using message types and priorities.

Advantages and differences:

  • sticky messages - a sticky message will be sticked and delivered in any case: either at once if the listener already exists, or immediately upon its registration.
  • observers priority - observers' priorities; you may set the notification priorities of your observers.
  • ordered messages - if a message is sent as ordered, an observer with a higher priority can cancel its further distribution simply by returning false.
  • you may set the execution context of the callback function for your observers.
  • flexible mechanism unsubscribe - you may unsubscribe by subscribe token, you may unsubscribe from the channel and the callback method, from the channel and the context, or simply unsubscribe the context from all channels - depending on your particular need.
  • tree-structured channels of message distribution - if you send a message to the channel level0.level1.level2, it will be first received by the subscribers of the channel level0, then level0.level1, and only then level0.level1.level2. Although, you may avoid this if you send a rootless message or a message to a channel that has no root.

##Use as a singleton: Since the main goal is the setup of the message bus in the application, it's desirable for this bus to be a singleton in the application.

var pubsub = require("../pubsub.js");

Or simply in the browser:

var pubsub = PubSub;

##Creating a new instance: Although you may create several buses, we don't see much sense in it.

var Constructor = require("../pubsub.js").Constructor;
var pubsub = new Constructor();

Or simply in the browser:

var pubsub = new PubSub.Constructor();

Methods

printLog

Takes true/false as the parameter. If set 'true', it writes the info about events and channels in the console.

pubsub.printLog(flag);

channels

Returns the array of the existing message channels, to which listeners are subscribed.

var channels  = pubsub.channels();

setSeparator

Sets a character or string, conveyed in the parameter sprtr for use as the separator of event subchannels.

pubsub.setSeparator(sprtr);

publish

Publishes the message on channel, delivering the object data to subscribers. It is possible to use the message type type.

pubsub.publish(channel, data, type);

Supported message types:

  • sticky - the message will be delivered to all modules subscribed to events in the actual channel, then 'sticked' for delivering it to new subscribers later on. Only the latest sticky message is saved in the system.
  • ordered - sends a message; you may stop its further distribution by returning false from the callback function of your observer. In case the message distribution is cancelled by the observer in the root channel, it will not be distributed to subchannels.
  • rootless - sends a message only to a selected channel without its roots.

subscribe

Subscribes the callback fuction fn to execution in context upon receiving the message by channel.

If you have a further need of unsubscribing a certain observer from a certain channel with a certain context - you may save the token that is returned by the subscription, in order to further unsubscribe with its help.

var token = pubsub.subscribe(channel, fn, context);

In order to set the priority of an observer, you must set the arrtibute priority in the callback function as any number which will define the priority - the higher the number, the higher the priority. If the attribute isn't set, the priority is minimal.

function callback() {
	// do smth
}
callback.priority = 1;

pubsub.subscribe('my_channel', callback);

Take notice that the attribute priority is deleted upon subscription; that's why its altering after the subscription to the channel does not affect priorities.

unsubscribe

Generally, unsubscription from a channel looks as follows:

pubsub.unsubscribe(channel, fn, context);

Unsubscribes from channel; valid for the subscribers registered with the callback function fn in context.

However, you may as well use shortened notations:

pubsub.unsubscribe('some.channel', callback);


* Unsubscribes from **channel**; valid for the subscribers registered with the callback function **fn** without defined context.

>```javascript
pubsub.unsubscribe('some.channel', context);
  • Unsubscribes from a selected channel; valid for the subscribers registered in context.

pubsub.unsubscribe(context);


* Unsubscribes from all channels; valid for all the subscribers registered in **context**.

>```javascript
pubsub.unsubscribe(channel);

Unsubscribes all subscribers from channel.

Examples

Basic example

// create a function to subscribe to channel
var subscriber = function( channel, message ){
    console.log( channel, message );
};

// add the function to the list of subscribers for a particular channel
PubSub.subscribe( 'my_channel', mySubscriber );

// publish messsage to channel
PubSub.publish( 'my_channel', { attr: 1} );

Advanced example

// create subscriber
var subscriber = {
	handleMsg: function (channel, message) {
		console.log( channel, message );
	}
}

// subscribe to several channels
PubSub.subscribe( 'channel_1', subscriber.handleMsg, subscriber );
PubSub.subscribe( 'channel_2', subscriber.handleMsg, subscriber );

// do smth

//unsubscribe this subscriber via context
PubSub.unsubscribe(subscriber);

Unsubscribe via token

// create a callback function to receive the messages
function subscriber(channel, message ){
    console.log( channel, message );
};

// add the function to the list of subscribers to a particular channel
// we're keeping the returned token, in order to be able to unsubscribe
// from the chanel later on
var token = PubSub.subscribe( 'my_channel', subscriber );

// unsubscribe this subscriber via token
PubSub.unsubscribe( token );

You can also review simple examples of using it in the test folder.