Installations
npm install citisys-graphql-mqtt-subscriptions
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
16.10.0
NPM Version
7.24.0
Score
47.7
Supply Chain
95.4
Quality
69.9
Maintenance
100
Vulnerability
98.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
davidyaha
Download Statistics
Total Downloads
271
Last Day
1
Last Week
1
Last Month
4
Last Year
51
GitHub Statistics
149 Stars
45 Commits
40 Forks
6 Watching
24 Branches
5 Contributors
Package Meta Information
Latest Version
1.3.0
Package Id
citisys-graphql-mqtt-subscriptions@1.3.0
Unpacked Size
113.74 kB
Size
19.63 kB
File Count
22
NPM Version
7.24.0
Node Version
16.10.0
Total Downloads
Cumulative downloads
Total Downloads
271
Last day
0%
1
Compared to previous day
Last week
0%
1
Compared to previous week
Last month
300%
4
Compared to previous month
Last year
-63.8%
51
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
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.
1schema { 2 query: Query 3 mutation: Mutation 4 subscription: Subscription 5} 6 7type Subscription { 8 somethingChanged: Result 9} 10 11type Result { 12 id: String 13}
Now, create a MQTTPubSub
instance.
1import { MQTTPubSub } from 'graphql-mqtt-subscriptions'; 2const pubsub = new MQTTPubSub(); // connecting to mqtt://localhost by default
Now, implement the Subscriptions type resolver, using pubsub.asyncIterator
to map the event you need.
1const SOMETHING_CHANGED_TOPIC = 'something_changed'; 2 3export const resolvers = { 4 Subscription: { 5 somethingChanged: { 6 subscribe: () => pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC) 7 } 8 } 9}
Subscriptions resolvers are not a function, but an object with
subscribe
method, that returnsAsyncIterable
.
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.
1pubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: { id: "123" }});
Dynamically Create a Topic Based on Subscription Args Passed on the Query:
1export const resolvers = { 2 Subscription: { 3 somethingChanged: { 4 subscribe: (_, args) => pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`), 5 }, 6 }, 7}
Using Arguments and Payload to Filter Events
1import { withFilter } from 'graphql-subscriptions'; 2 3export const resolvers = { 4 Subscription: { 5 somethingChanged: { 6 subscribe: withFilter( 7 (_, args) => pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`), 8 (payload, variables) => payload.somethingChanged.id === variables.relevantId, 9 ), 10 }, 11 }, 12}
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.
1import { connect } from 'mqtt'; 2import { MQTTPubSub } from 'graphql-mqtt-subscriptions'; 3 4const client = connect('mqtt://test.mosquitto.org', { 5 reconnectPeriod: 1000, 6}); 7 8const pubsub = new MQTTPubSub({ 9 client 10});
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.
1const triggerToQoSMap = { 2 'comments.added': 1, 3 'comments.updated': 2, 4}; 5 6const pubsub = new MQTTPubSub({ 7 publishOptions: trigger => Promise.resolve({ qos: triggerToQoSMap[trigger] }), 8 9 subscribeOptions: (trigger, channelOptions) => Promise.resolve({ 10 qos: Math.max(triggerToQoSMap[trigger], channelOptions.maxQoS), 11 }), 12});
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
1const onMQTTSubscribe = (subId, granted) => { 2 console.log(`Subscription with id ${subId} was given QoS of ${granted.qos}`); 3} 4 5const pubsub = new MQTTPubSub({onMQTTSubscribe});
Change Encoding Used to Encode and Decode Messages
Supported encodings available here
1const pubsub = new MQTTPubSub({ 2 parseMessageWithEncoding: 'utf16le', 3});
Basic Usage with SubscriptionManager (Deprecated)
1import { MQTTPubSub } from 'graphql-mqtt-subscriptions'; 2const pubsub = new MQTTPubSub(); // connecting to mqtt://localhost on default 3const subscriptionManager = new SubscriptionManager({ 4 schema, 5 pubsub, 6 setupFunctions: {}, 7});
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.
1const triggerTransform = (trigger, { path }) => [trigger, ...path].join('.');
Note that a
path
field to be passed to thechannelOptions
but you can do whatever you want.
Next, pass the triggerTransform
to the MQTTPubSub
constructor.
1const pubsub = new MQTTPubSub({ 2 triggerTransform, 3});
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.
1const subscriptionManager = new SubscriptionManager({ 2 schema, 3 setupFunctions: { 4 commentsAdded: (options, { repoName }) => ({ 5 'comments/added': { 6 channelOptions: { path: [repoName] }, 7 }, 8 }), 9 }, 10 pubsub, 11});
Note that the
triggerTransform
dependency on thepath
field is satisfied here.
When subscribe
is called like this:
1const query = ` 2 subscription X($repoName: String!) { 3 commentsAdded(repoName: $repoName) 4 } 5`; 6const variables = {repoName: 'graphql-mqtt-subscriptions'}; 7subscriptionManager.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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 5/9 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 26 are checked with a SAST tool
Reason
38 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-fwr7-v2mv-hh25
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-pp7h-53gx-mx7r
- Warn: Project is vulnerable to: GHSA-9vvw-cc9w-f27h
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-h6ch-v84p-w6p9
- Warn: Project is vulnerable to: GHSA-qrmc-fj45-qfc2
- Warn: Project is vulnerable to: GHSA-qh2h-chj9-jffq
- Warn: Project is vulnerable to: GHSA-q42p-pg8m-cqh6
- Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9
- Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f
- Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p
- Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv
- Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8
- Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65
- Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh
- Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44
- Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988
- Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-h9mj-fghc-664w
- Warn: Project is vulnerable to: GHSA-wv67-9jq7-8r69
- Warn: Project is vulnerable to: GHSA-w9mr-4mfr-499f
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-7p7h-4mm5-852v
- Warn: Project is vulnerable to: GHSA-5v72-xg48-5rpm
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Score
2.4
/10
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More