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

snub

v3.0.4

Published

pub/sub

Downloads

23

Readme

Snub

Pub Sub message system, supports middleware, single delivery, replys and clusterable.

Usage

npm install snub

Tests

Tests are few but cover the essentials, feel free to add to them. Redis needs to be running no auth on default port. npm run redis - basic redis docker npm run test

Basic Example

With redis installed and running with default port and no auth.

const Snub = require('snub');
const snub = new Snub();

// create listener for 'hello'
snub.on('hello', (payload) => {
  console.log('Recieved => ', payload);
});

// send 'world' to single 'hello' listener.
snub.mono('hello', 'world').send();

For further examples take a look in examples.

Advanced Setup

Optional config, defaults are applied if omitted.

const Snub = require('snub');
const snub = new Snub({
    prefix: 'snub', // prefix all snub pub/sub messages
    port: 6379, // redis port ! DEPRICATED !
    host: '127.0.0.1', // redis host ! DEPRICATED !
    auth: null,   // redis auth ! DEPRICATED !
    debug: false, // dump debug junk
    timeout: 5000 // reply timeout, we cant listen forever,
    monoWait: 50,  // used to help mono delivery messages to get dispersed evenlyish, this is a max wait time. will randomize between 0-monoWait, if you have small amount of instances set this low. poly does not use this value.
    redisAuth: { // see https://github.com/redis/ioredis#connect-to-redis for more information
      port: 6379, // Redis port
      host: "127.0.0.1", // Redis host
      username: "default", // needs Redis >= 6
      password: "my-top-secret",
      db: 0, // Defaults to 0
    } || 'redis://:[email protected]:6380/4' || 'redis://username:[email protected]:6380/4',
    redisStore: { // optional, if you would like the store be on a seperate instanc from pub/sub activity
      port: 6379, // Redis port
      host: "127.0.0.1", // Redis host
      username: "default", // needs Redis >= 6
      password: "my-top-secret",
      db: 0, // Defaults to 0
    } || 'redis://:[email protected]:6380/4' || 'redis://username:[email protected]:6380/4'
    },
    intercepter: async (payload, reply, channel, pattern) => {
      // payload be mutated directly, reply fn can be called early. (if you reply and return true you might have an uninteded outcome)
      return true; // returning false will not exectute the listener.
    }
  });

API

snub.on('eventname', (payload, [reply]) => {});
  • Method is run when an event is triggered.
  • Use patterns in subscribers like so 'h[ae]llo' or hell*
  • Reply is optionaly a function to reply at with a single param. reply({})
  • you can also namespace events hello.one to allow easy removal with snub.off('hello.one')
snub.once('eventname', (payload, [reply]) => {});

Same as .on but event will only trigger once.

snub.off('eventname');
  • Remove all event listeners by name
  • append namespace to remove single listener if you have mutliple.
snub.poly('eventname', payload).send([function]);

Emits an event trigger with the payload .send() accepts and optional callback function which carries a number value which will indicate the amount of listeners that heard the event trigger. Poly will deliver the message and payload to all listeners.

snub.poly('eventname').replyAt(function(replyData, error), [optional timeout]).send([function]);

Allows the event to be replied to, since this will get delivered to all listeners the reply will prossibly be responded to multiple times.

snub.mono('eventname', payload).send([function]);

Emits an event trigger with the payload .send() accepts and optional callback function which carries a number value which will indicate the amount of listeners that heard the event trigger (will always be 0 or 1 for mono). Mono will only be delivered to a single listener, this is first come first served, there is not methodology or way to control this as of yet, think of it as random delivery.

snub.mono('eventname', payload).sendDelay(5);

number of seconds to delay the event being sent. this is stores in redis so will persist restarts.

snub.poly('eventname', payload).replyAt(function(), [optional timeout]).send([function]);

Allows the event to be replied to, reply can only run once.

snub.use(middlewareMethod);

Snub accepts middleware, it will hand the snub instance to the middleware method.

Some handy middleware