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 🙏

© 2026 – Pkg Stats / Ryan Hefner

jspubsub

v1.1.1

Published

An implementation of Publish Subscribe pattern in JavaScript, in both browser and server compatible versions

Readme

PubSub

An implementation of Publish Subscribe pattern in JavaScript, in both browser and server compatible versions

Setup

Browser

<script src="pubsub.js"></script>

Server (requires node version 4 or higher)

Install from npm register

npm install jspubsub

Import or require in your module

import PubSub from 'jspubsub';
const PubSub = require('jspubsub');

Usage

Set a new subscriber object calling the Subscribe class with a topic and a callback assigned as parameters

let subscriber = new PubSub.Subscribe('test', (data) => {
	console.log(data.test)
});

The executor callback will be fired when the static 'publish' method is called with the related toipc as first argument. You can pass moreover arbitrary object data as the second parameter

PubSub.publish('test', { test: 'very nice test' });
// console logs 'very nice test'

Singleton

You can subscribe events to be fired once

let subscriber = new PubSub.SubscribeOnce('test', () => {
	console.log('this will be printed once')
});

for (let i = 0; i < 5; i++) {
	PubSub.publish('test');
}
// console logs 'this will be printed once' just once

Note about async and sync publishing

PubSub's publish method has been designed to be asynchronous, so topic published will not block the main thread and your program will be more predictable. If you want instead to do actions that need to be executend soon there is a 'publishSync' method:

let result = null;

let subscriber = new PubSub.Subscribe('test', (data) => {
	result = data.test;
});

PubSub.publish('test', { test: 'very nice test' });
console.log(result) // null, beacuse execution of the topic is async so the callback has been pushed at the end of the queue

PubSub.publishSync('test', { test: 'very nice test' });
console.log(result) // 'very nice test'

Working with the subscriber instance

Count getter

Every instance of PubSub.Subscribe class has a getter that expose the number of times the topic has been called, you might find this useful for debugging:

let subscriber = new PubSub.Subscribe('test', (data) => {
	console.log(data.test)
});
PubSub.publishSync('test', { test: 'very nice test' });
for (let i = 0; i < 5; i++) {
	PubSub.publish('test');
}
console.log(subscriber.count) // 5

Note that we have used 'publishSync' beacuse we wanted to check the actual count within the current thread, so changed caused by the topic's publishing had to be triggered and executed immediately.

Remove the subscription

With the 'remove' method, when called we disconnect the subscriber from listening the topic it was subscribed to:

let subscriber = new PubSub.Subscribe('test', (data) => {
	console.log(data.test)
});
subscriber.remove();
PubSub.publish('test', { test: 'very nice test' });
// console logs nothing

Tests

You can find tests in test/test.js Install dependencies first with npm install and run tests with npm test command.