This package implements the AsyncIterator Interface and PubSubEngine Interface from the graphql-subscriptions package. It allows you to connect your subscriptions manager to the Solace PubSub+ event broker to support a horizontally scalable subscriptions setup. Solace is the world's most advanced event broker that is available for free and offers innovative features such as event mesh, topic routing, and unparalleled performance. For a full working example of this package working with Solace, head over to this repo.
npm install @solace-community/graphql-solace-subscriptions
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 SolacePubSub
instance.
import { SolacePubSub } from '@solace-community/graphql-solace-subscriptions';
const pubsub = await SolacePubSub.startWithDefaultOptions("GRAPH_QL_QUEUE"); // connecting to ws://localhost:8080 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 returnsAsyncIterable
.
The AsyncIterator
method will tell the Solace client to listen for messages from the Solace broker on the topic provided via a Queue, 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" }});
export const resolvers = {
Subscription: {
somethingChanged: {
subscribe: (_, args) => pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}/${args.relevantId}`),
},
},
}
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,
),
},
},
}
The basic usage is great for development and you will be able to connect to a locally hosted Solace PubSub+ event broker. But if you wanted to connect to a specific host, you can inject your own Solace properties.
import { SolacePubSub, SolacePubSubOptions } from '@solace-community/graphql-solace-subscriptions';
let solacePubSubOptions = new SolacePubSubOptions("wss://host:8081","vpn1","user","password");
const pubsub = await SolacePubSub.startWithSolaceOptions("GRAPH_QL_QUEUE",solacePubSubOptions);
If you want to take advantage of a different authentication mechanism, you have the ability to inject a fully instantiated Solace session into the SolacePubSub object.
import { SolacePubSub } from '@solace-community/graphql-solace-subscriptions';
import solace from 'solclientjs';
let session: solace.Session;
//instantiate your solace session
const pubsub = await SolacePubSub.startWithSolaceSession("GRAPH_QL_QUEUE",session);
This is not an officially supported Solace product.
For more information try these resources:
- Ask the Solace Community
- The Solace Developer Portal website at: https://solace.dev
Contributions are encouraged! Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
See the list of contributors who participated in this project.
See the LICENSE file for details.