npm

@aerogear/graphql-mqtt-subscriptions

graphql-mqtt-subscriptions

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';

constpubsub=newMQTTPubSub();// connecting to mqtt://localhost by default

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

constSOMETHING_CHANGED_TOPIC='something_changed';

exportconstresolvers={

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.

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.

Change Encoding Used to Encode and Decode Messages

Basic Usage with SubscriptionManager (Deprecated)

constpubsub=newMQTTPubSub();// connecting to mqtt://localhost on default

constsubscriptionManager=newSubscriptionManager({

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.

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.

constpubsub=newMQTTPubSub({

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.

constsubscriptionManager=newSubscriptionManager({

schema,

setupFunctions:{

commentsAdded:(options,{ repoName })=>({

'comments/added':{

channelOptions:{ path:[repoName]},

},

}),

},

pubsub,

});

Note that the triggerTransform dependency on the path field is satisfied here.

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.