All Projects → davidyaha → Graphql Mqtt Subscriptions

davidyaha / Graphql Mqtt Subscriptions

Licence: mit
graphql-subscriptions implementation for MQTT protocol

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Graphql Mqtt Subscriptions

Aws Lambda Graphql
Use AWS Lambda + AWS API Gateway v2 for GraphQL subscriptions over WebSocket and AWS API Gateway v1 for HTTP
Stars: ✭ 313 (+135.34%)
Mutual labels:  graphql, graphql-subscriptions
Kikstart Graphql Client
🚀 Small NodeJS Wrapper around apollo-client that provides easy access to running queries, mutations and subscriptions.
Stars: ✭ 27 (-79.7%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Yoga
🧘 Fully-featured GraphQL Server with focus on easy setup, performance & great developer experience
Stars: ✭ 6,573 (+4842.11%)
Mutual labels:  graphql, graphql-subscriptions
Aws Mobile Appsync Sdk Ios
iOS SDK for AWS AppSync.
Stars: ✭ 231 (+73.68%)
Mutual labels:  graphql, graphql-subscriptions
React Fullstack Graphql
Starter projects for fullstack applications based on React & GraphQL.
Stars: ✭ 1,352 (+916.54%)
Mutual labels:  graphql, graphql-subscriptions
Morpheus Graphql
Haskell GraphQL Api, Client and Tools
Stars: ✭ 285 (+114.29%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Redis Subscriptions
A graphql subscriptions implementation using redis and apollo's graphql-subscriptions
Stars: ✭ 829 (+523.31%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Kafka Subscriptions
Apollo graphql subscriptions over Kafka protocol
Stars: ✭ 154 (+15.79%)
Mutual labels:  graphql, graphql-subscriptions
Angular Fullstack Graphql
🚀 Starter projects for fullstack applications based on Angular & GraphQL.
Stars: ✭ 92 (-30.83%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Rxjs
fork of Graphql which adds Observable support
Stars: ✭ 78 (-41.35%)
Mutual labels:  graphql, graphql-subscriptions
36 Graphql Concepts
📜 36 concepts every GraphQL developer should know.
Stars: ✭ 209 (+57.14%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Google Pubsub
A graphql-subscriptions PubSub Engine using Google PubSub
Stars: ✭ 108 (-18.8%)
Mutual labels:  graphql, graphql-subscriptions
Djangochannelsgraphqlws
Django Channels based WebSocket GraphQL server with Graphene-like subscriptions
Stars: ✭ 203 (+52.63%)
Mutual labels:  graphql, graphql-subscriptions
Altair
✨⚡️ A beautiful feature-rich GraphQL Client for all platforms.
Stars: ✭ 3,827 (+2777.44%)
Mutual labels:  graphql, graphql-subscriptions
Grial
A Node.js framework for creating GraphQL API servers easily and without a lot of boilerplate.
Stars: ✭ 194 (+45.86%)
Mutual labels:  graphql, graphql-subscriptions
Aws Mobile Appsync Sdk Js
JavaScript library files for Offline, Sync, Sigv4. includes support for React Native
Stars: ✭ 806 (+506.02%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Genie
Simply pass in your GraphQL type defintions and get a fully featured GraphQL API with referential integrity, inverse updates, subscriptions and role based access control that can be used client side or server side.
Stars: ✭ 147 (+10.53%)
Mutual labels:  graphql, graphql-subscriptions
Tutorial Graphql Subscriptions Redis
GraphQL server implementation with Redis backing allowing pubsub
Stars: ✭ 12 (-90.98%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Subscriptions
📰 A small module that implements GraphQL subscriptions for Node.js
Stars: ✭ 1,390 (+945.11%)
Mutual labels:  graphql, graphql-subscriptions
Graphql Postgres Subscriptions
A graphql subscriptions implementation using postgres and apollo's graphql-subscriptions
Stars: ✭ 133 (+0%)
Mutual labels:  graphql, graphql-subscriptions

graphql-mqtt-subscriptions

Greenkeeper badge

This package implements the AsyncIterator Interface and PubSubEngine Interface from the graphql-subscriptions package. It allows you to connect your subscriptions manager to an MQTT enabled Pub Sub broker to support

horizontally scalable subscriptions setup. This package is an adapted version of my graphql-redis-subscriptions package.

Installation

npm install graphql-mqtt-subscriptions

Using the AsyncIterator Interface

Define your GraphQL schema with a Subscription type.

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

type Subscription {
    somethingChanged: Result
}

type Result {
    id: String
}

Now, create a MQTTPubSub instance.

import { MQTTPubSub } from 'graphql-mqtt-subscriptions';
const pubsub = new MQTTPubSub(); // connecting to mqtt://localhost by default

Now, implement the Subscriptions type resolver, using 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.

The AsyncIterator method will tell the MQTT client to listen for messages from the MQTT broker on the topic provided, and wraps that listener in an AsyncIterator object.

When messages are received from the topic, those messages can be returned back to connected clients.

pubsub.publish can be used to send messages to a given topic.

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

Dynamically Create 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 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,
      ),
    },
  },
}

Passing your own client object

The basic usage is great for development and you will be able to connect to any mqtt enabled server running on your system seamlessly. For production usage, it is recommended you pass your own MQTT client.

import { connect } from 'mqtt';
import { MQTTPubSub } from 'graphql-mqtt-subscriptions';

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

const pubsub = new MQTTPubSub({
  client
});

You can learn more on the mqtt options object here.

Changing QoS for publications or subscriptions

As specified here, the MQTT.js publish and subscribe functions takes an options object. This object can be defined per trigger with publishOptions and subscribeOptions resolvers.

const triggerToQoSMap = {
  'comments.added': 1,
  'comments.updated': 2,
};

const pubsub = new MQTTPubSub({
  publishOptions: trigger => Promise.resolve({ qos: triggerToQoSMap[trigger] }),
  
  subscribeOptions: (trigger, channelOptions) => Promise.resolve({ 
    qos: Math.max(triggerToQoSMap[trigger], channelOptions.maxQoS), 
  }),
});

Get Notified of the Actual QoS Assigned for a Subscription

MQTT allows the broker to assign different QoS levels than the one requested by the client. In order to know what QoS was given to your subscription, you can pass in a callback called onMQTTSubscribe

const onMQTTSubscribe = (subId, granted) => {
  console.log(`Subscription with id ${subId} was given QoS of ${granted.qos}`);
}

const pubsub = new MQTTPubSub({onMQTTSubscribe});

Change Encoding Used to Encode and Decode Messages

Supported encodings available here

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

Basic Usage with SubscriptionManager (Deprecated)

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

Using Trigger Transform (Deprecated)

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

Here is an example of a generic trigger transform.

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

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

Next, pass the triggerTransform to the MQTTPubSub constructor.

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

Lastly, a setupFunction is provided for the 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 the triggerTransform dependency on the path field is satisfied here.

When subscribe is called like this:

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

The subscription string that MQTT will receive will be comments.added.graphql-mqtt-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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].