@aaronpowell/graphql-signalr-subscriptions

A GraphQL subscriptions library using Azure SignalR Service

Usage no npm install needed!

<script type="module">
  import aaronpowellGraphqlSignalrSubscriptions from 'https://cdn.skypack.dev/@aaronpowell/graphql-signalr-subscriptions';
</script>

README

SignalR GraphQL Subscriptions npm - SignalR package

This package contains support for Apollo GraphQL Subscriptions, using the Azure SignalR Service.

Installation

Install via npm or GitHub Packages:

gt; npm install --save @aaronpowell/graphql-signalr-subscriptions

Usage

You'll need a SignalR Service account (if you don't have an Azure account sign up for free). Copy the connection string and provide it when you create an instance of SignalRPubSub:

import { SignalRPubSub } from "@aaronpowell/graphql-signalr-subscriptions";

const signalrPubSub = new SignalRPubSub("<connection string>");

When your server is ready, you can start the pubsub client:

signalrPubSub
  .start()
  .then(() => console.log("SignalR PubSub is up and running"))
  .catch((error) => console.error(error));

From within a resolver, you use it the same as any other pubsub implementation:

export const resolvers = {
  Query: {
    hello() {
      signalrPubSub.publish("TESTING", { getMessage: "Hello I'm a message" });
      return "Some message";
    },
  },
  Subscription: {
    getMessage: {
      subscribe: () => signalrPubSub.asyncIterator(["TESTING"]),
    },
  },
};