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-ably-pubsub

v1.0.0

Published

A graphql-subscriptions PubSub Engine using Ably

Downloads

109

Readme

graphql-ably-pubsub

Ably is a pub/sub messaging platform with a suite of integrated services to deliver complete realtime functionality directly to end-users. In the context of GraphQL, we can use it to publish when a mutation is fired and subscribe to the result through a subscription query.

This package implements the PubSubEngine Interface from the graphql-subscriptions package and also the new AsyncIterator interface. It allows you to connect your subscription manger to an Ably PubSub mechanism to support multiple subscription manager instances.

Installation

npm install graphql-ably-pubsub or yarn add graphql-ably-pubsub

Using as AsyncIterator

Define your GraphQL schema with a Subscription type:

schema {
  query: Query
  mutation: Mutation
  subscription: Subscription
}

type Subscription {
  somethingChanged: Result
}

type Result {
  id: String
}

Now, let's create a simple AblyPubSub instance:

import { AblyPubSub } from "graphql-ably-pubsub";
const pubsub = new AblyPubSub();

Now, implement your Subscriptions type resolver, using the pubsub.asyncIterator to map the event you need:

const SOMETHING_CHANGED_TOPIC = "something_changed";

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),
    },
  },
};

Subscriptions resolvers are not a function, but an object with subscribe method, that returns AsyncIterable.

Calling the method asyncIterator of the AblyPubSub instance will subscribe to the topic provided and will return an AsyncIterator bound to the AblyPubSub instance and listens to any event published on that topic. Now, the GraphQL engine knows that somethingChanged is a subscription, and every time we will use pubsub.publish over this topic, the AblyPubSub will PUBLISH the event to all other subscribed instances and those in their turn will emit the event to GraphQL using the next callback given by the GraphQL engine.

pubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: { id: "123" } });

Dynamically use a topic based on subscription args passed on the query:

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: (_, args) =>
        pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`),
    },
  },
};

Using both arguments and payload to filter events

import { withFilter } from "graphql-subscriptions";

export const resolvers = {
  Subscription: {
    somethingChanged: {
      subscribe: withFilter(
        (_, args) =>
          pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`),
        (payload, variables) =>
          payload.somethingChanged.id === variables.relevantId
      ),
    },
  },
};

Creating the Ably Client

import { AblyPubSub } from "graphql-ably-pubsub";

const pubSub = new AblyPubSub(options, channelName, pubSubClient);

Options

These are the options which are passed to the internal or passed Ably PubSub client. Example -

const options = {
  key: "<YOUR-ABLY-API-KEY>",
};

channelName (optional)

If specified, this channel name is used for every trigger otherwise the trigger itself is used as the channel name. Example -

const channel = "ably-subscription-channel";

pubSubClient (optional)

If specified, then this client will be used and the options param value will be ignored. Example -

const options = {
  key: "<YOUR-ABLY-API-KEY>",
};
const pubSubClient = new Ably.Realtime(options);

Acknowledgements

This project is mostly inspired by graphql-redis-subscriptions and graphql-google-pubsub. Thanks to its authors for their work and inspiration.