Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-subscriptions

Package Overview
Dependencies
Maintainers
1
Versions
40
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-subscriptions

GraphQL subscriptions for node.js

  • 3.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
807K
decreased by-3.81%
Maintainers
1
Weekly downloads
 
Created

What is graphql-subscriptions?

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.

What are graphql-subscriptions's main functionalities?

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

Other packages similar to graphql-subscriptions

FAQs

Package last updated on 11 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc