Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
graphql-subscriptions
Advanced tools
The graphql-subscriptions package provides a simple way to add real-time capabilities to your GraphQL server using subscriptions. It allows you to subscribe to specific events and get notified when those events occur, enabling real-time updates in your applications.
PubSub Implementation
The PubSub implementation allows you to create a simple publish-subscribe mechanism. You can publish events and subscribe to them using the PubSub class.
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();
// Publishing an event
pubsub.publish('EVENT_NAME', { data: 'some data' });
// Subscribing to an event
const subscription = pubsub.asyncIterator('EVENT_NAME');
GraphQL Subscription Setup
This code demonstrates how to set up a GraphQL subscription using the graphql-subscriptions package. It defines a subscription type and uses the PubSub instance to handle the subscription logic.
const { GraphQLObjectType, GraphQLSchema, GraphQLString } = require('graphql');
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();
const SubscriptionType = new GraphQLObjectType({
name: 'Subscription',
fields: {
messageSent: {
type: GraphQLString,
subscribe: () => pubsub.asyncIterator('MESSAGE_SENT')
}
}
});
const schema = new GraphQLSchema({
subscription: SubscriptionType
});
Triggering Subscriptions
This feature shows how to trigger a subscription event. The sendMessage function publishes an event that can be subscribed to by clients.
const { PubSub } = require('graphql-subscriptions');
const pubsub = new PubSub();
// Function to trigger a subscription
function sendMessage(message) {
pubsub.publish('MESSAGE_SENT', { messageSent: message });
}
// Example usage
sendMessage('Hello, world!');
The subscriptions-transport-ws package provides a WebSocket-based transport for GraphQL subscriptions. It is more focused on the transport layer and integrates well with Apollo Server. Compared to graphql-subscriptions, it offers more control over the WebSocket connection and supports advanced features like connection parameters and custom message handlers.
The graphql-ws package is a lightweight and modern WebSocket implementation for GraphQL subscriptions. It is designed to be simple and efficient, with a focus on performance and ease of use. Compared to graphql-subscriptions, it provides a more streamlined API and better performance for high-throughput applications.
The graphql-yoga package is a fully-featured GraphQL server that includes built-in support for subscriptions. It is designed to be easy to set up and use, with sensible defaults and a focus on developer experience. Compared to graphql-subscriptions, it offers a more integrated solution with less boilerplate code required to get started.
GraphQL subscriptions is a simple npm package that lets you wire up GraphQL with a pubsub system (like Redis) to implement subscriptions in GraphQL.
npm install graphql-subscriptions
import { PubSub, SubscriptionManager } from 'graphql-subscriptions';
import schema from './schema';
// PubSub can be easily replaced, for example with https://github.com/davidyaha/graphql-redis-subscriptions
const pubsub = new PubSub();
const subscriptionManager = new SubscriptionManager({
schema,
pubsub,
// setupFunctions maps from subscription name to a map of channel names and their filter functions
// in this case it will subscribe to the commentAddedChannel and re-run the subscription query
// every time a new comment is posted whose repository name matches args.repoFullName.
setupFunctions: {
commentAdded: (options, args) => ({
newCommentsChannel: {
filter: comment => comment.repository_name === args.repoFullName,
},
}),
},
});
// start a subscription
subscriptionManager.subscribe({
query: `
subscription newComments($repoName: String!){
commentAdded(repoName: $repoName) { # <-- this is the subscription name
id
content
createdBy {
username
}
}
}
`,
variables: {
repoName: 'apollostack/GitHunt-API',
},
context: {},
callback: (err, data) => console.log(data),
});
// publish to the channel
pubsub.publish('newCommentsChannel', {
id: 123,
content: 'Test',
repoFullName: 'apollostack/GitHunt-API',
posted_by: 'helfer',
});
// the query will run, and the callback will print
// {
// data: {
// commentAdded: {
// id: 123,
// content: 'Test',
// createdBy: {
// username: 'helfer',
// }
// }
// }
// }
0.2.2
graphql
a peer dependency and updated it to 0.8.2FAQs
GraphQL subscriptions for node.js
The npm package graphql-subscriptions receives a total of 782,813 weekly downloads. As such, graphql-subscriptions popularity was classified as popular.
We found that graphql-subscriptions demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.