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

subscriptionmanager

v0.1.2

Published

Manage subscriptions to Redis Pub/Sub channels

Downloads

4

Readme

Subscription Manager

A handy little tool for subscribing objects to Redis Pub/Sub channels.

npm install subscriptionmanager

The original idea was to connect WebSocket clients to Pub/Sub channels with events being fired by background processes. For example, notify a client when a background process finishes encoding a video. Or, notify a client when their web-connected factory produces another item.

Usage

var redis = require('redis').createClient();
var SM = require('SubscriptionManager');

var subMan = new SM(redis);

// Do whatever you want with the clients when a message comes in.
subMan.on('message', function(channel, clients, message) {
  console.log('Received a message for: %s', clients);
  console.log('Listening on channel: %s', channel);
  console.log('Saying: %s', message);

  clients.forEach(function(client) {
    client.notify(message);
  });
});

// Subscribe a client to a channel
subMan.subscribe(myClient, 'myChannel');

// List clients subscribed to a channel
subMan.getSubscriptions('myChannel'); // -> [myClient]

// List channels with active subscriptions
subMan.getChannels(); // -> ['myChannel']

// Unsubscribe a client from a channel
subMan.unsubscribe(myClient, 'myChannel');

API

new SubscriptionManager(redis, options)

Creates a new subscription manager. Possible options include:

  • prefix: Adds a prefix to your channels
var sm = new SM(redis, {prefix: 'Channel'});

// This will subscribe client to 'Channel:myChannel'
sm.subscribe(client, 'myChannel');

sm.subscribe(client, channel)

Subscribes a client to a given channel. Will also issue a Redis SUBSCRIBE if this is the first client to subscribe to this channel.

sm.unsubscribe(client, channel)

Remove a client from a given channel. If no more clients are subscribed to this channel, it will issue a Redis UNSUBSCRIBE for this channel.

sm.getSubscriptions(channel)

Returns an array of clients that are subscribed to this channel.

sm.getChannels()

Returns an array of all channels that currently have a client subscribed to them.

Events

"message" (channel, clients, message)

The subscription manager will emit message events anytime a message is received from any of the subscribed channels.

  • channel: String for which channel received the event, the prefix will be included on the channel.
  • clients: Array of clients that are subscribed to this channel.
  • message: The message the was published

Redis Connection

Subscription Manager depends only on having a node_redis style interface for the Redis object. Feel free to replace it with your own implementation. The only methods that are used are:

  • redis.subscribe(channel)
  • redis.unsubscribe(channel)
  • redis.on('message', function(channel, message) {})

In addition, debugging uses:

  • redis.on('subscribe', function(channel, count) {})
  • redis.on('unsubscribe' function(channel, count) {})

However, these are not necessary to implement (no functionality depends on them).

Debugging

We're using debug for debug output here.

To see what's going on under the hood, set the DEBUG environment variable to SubscriptionManager when you run node.

DEBUG=SubscriptionManager node file.js

# --- OR ---

export DEBUG=SubscriptionManager
node file.js

To Do

  • [ ] Add ability to unsubscribe from all channels simultaneously
  • [ ] Get a list of channels for a given client