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

graphql-nats-subscriptions

v1.5.0

Published

This package implements the PusSubEngine Interface from the graphql-subscriptions package. It allows you to connect your subscriptions manger to an NATS enabled Pub Sub broker to support horizontally scalable subscriptions setup. This package is an adap

Downloads

1,517

Readme

graphql-nats-subscriptions

This package implements the PusSubEngine Interface from the graphql-subscriptions package. It allows you to connect your subscriptions manger to an NATS enabled Pub Sub broker to support horizontally scalable subscriptions setup. This package is an adapted version of graphql-redis-subscriptions package.

Basic Usage

import { NatsPubSub } from 'graphql-nats-subscriptions';
const pubsub = new NatsPubSub(); // connecting to nats://localhost on default
const subscriptionManager = new SubscriptionManager({
  schema,
  pubsub,
  setupFunctions: {},
});

You needs gnatsd daemon running in background. Check out https://nats.io to start on your machine.

Using Trigger Transform

As the graphql-redis-subscriptions package, this package support a trigger transform function. This trigger transform allows you to use the channelOptions object provided to the SubscriptionManager instance, and return trigger string which is more detailed then the regular trigger.

First I create a simple and generic trigger transform

const triggerTransform = (trigger, {path}) => [trigger, ...path].join('.');

Note that I expect a path field to be passed to the channelOptions but you can do whatever you want.

Next, I'll pass the triggerTransform to the NatsPubSub constructor.

const pubsub = new NatsPubSub({
  triggerTransform,
});

Lastly, I provide a setupFunction for commentsAdded subscription field. It specifies one trigger called comments.added and it is called with the channelOptions object that holds repoName path fragment.

const subscriptionManager = new SubscriptionManager({
  schema,
  setupFunctions: {
    commentsAdded: (options, {repoName}) => ({
      'comments/added': {
        channelOptions: { path: [repoName] },
      },
    }),
  },
  pubsub,
});

Note that here is where I satisfy my triggerTransform dependency on the path field.

When I call subscribe like this:

const query = `
  subscription X($repoName: String!) {
    commentsAdded(repoName: $repoName)
  }
`;
const variables = {repoName: 'graphql-redis-subscriptions'};
subscriptionManager.subscribe({ query, operationName: 'X', variables, callback });

The subscription string that Redis will receive will be comments.added.graphql-redis-subscriptions. This subscription string is much more specific and means the the filtering required for this type of subscription is not needed anymore. This is one step towards lifting the load off of the graphql api server regarding subscriptions.

Passing your own client object

The basic usage is great for development and you will be able to connect to any nats enabled server running on your system seamlessly. But for any production usage you should probably pass in your own configured client object;

import { connect } from 'nats';
import { NatsPubSub } from 'graphql-nats-subscriptions';

const client = connect('nats://test.mosquitto.org', {
  reconnectPeriod: 1000,
});

const pubsub = new NatsPubSub({
  client,
});

You can learn more on the nats options object here.

Change encoding used to encode and decode messages

Supported encodings available here

const pubsub = new NatsPubSub({
  parseMessageWithEncoding: 'utf16le',
});

License

graphql-nats-subscriptions is open source under the MIT license