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

exchange-pubsub

v2.0.0

Published

A wrapper to simplify basic pubsub usage

Downloads

55

Readme

Version Build Status

exchange-pubsub

A helper module to simplify @google-cloud/pubsub

Installation

npm i --save exchange-pubsub

Options

  • log = logger to use (defaults to console, set to false to disable)
  • defaultSubscribeOptions = (same as optional per-subscription options)
    • raw - provide full message to listener. Default = false (just the data)
    • autoAck - automatically acknowledge messages on return from listener. Default = true
    • subNameWithTopic - automatically add topic name to subscription name. Default = true
    • connectionInterval - automatically resubscribe every xx seconds of inactivity. 0 = off (default).
  • ignoreKeyFilename = don't set default pubSub keyFilename option
  • pubSub = see @google-cloud/pubsub
    • projectId will attempt to use process.env.GCLOUD_PROJECT if not set
    • keyFilename will set to 'lib/gcloud-auth.json' if not set and ignoreKeyFilename is not set
    • flowControl: maxMessages = 50 by default

Usage

const pubSub = require('exchange-pubsub');
// Optional:
pubSub.setOptions({
  log: console,
  defaultSubscribeOptions: {
    raw: false,
    autoAck: true,
  },
});

// simple usage:
pubSub.subscribe('myTopic', msgData => {
  // default is only the message.data property from pubsub
  console.log(msgData); // should be 'my message' in this example
  
  // optionally return false or a Promise
  // if return value resolves to false, message will be nack'd, else ack'd
  // (unless autoAck option is disabled)
});

pubSub.publish('myTopic', 'my message')
  .then(() => {/* do something */ })
  .catch(e => {/* error */ });

The default usage uses the topic as the subscription name which means it will be processed by a single subscriber (if multiple subscribers use the same name).

You can also specify the name manually, or have a random name generated:

// specify subscription name and options:
pubSub.subscribe('myTopic', 'subscription name', {autoAck: false, raw: true}, (msg => {
  console.log(msg.data);
  msg.ack();
}));

// random subscription name
pubSub.subscribe('myTopic', true, (msgData => {
  console.log(msgData);
  return Promise.reject();  // force a nack
}));

// CAUTION: careful with binding...
// DON'T DO: pubSub.subscribe('myTopic', true, myClass.function)
// Instead, use one of the following:
pubSub.subscribe('myTopic', 'subName', (msg) => myClass.function(msg));
pubSub.subscribe('myTopic', 'subName', myClass.function.bind(myClass));

Source: demo.js