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

graphql-redis-subscriptions

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-redis-subscriptions

A graphql-subscriptions PubSub Engine using redis

  • 2.7.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created

What is graphql-redis-subscriptions?

The graphql-redis-subscriptions package is a GraphQL subscription manager that uses Redis as a Pub/Sub mechanism. It allows you to implement real-time features in your GraphQL API by leveraging Redis for message passing and event handling.

What are graphql-redis-subscriptions's main functionalities?

Basic Subscription Setup

This code demonstrates how to set up a basic subscription using graphql-redis-subscriptions. It defines a GraphQL schema with a subscription type and sets up a resolver to handle the subscription. The pubsub.publish method is used to publish events to the subscription.

const { RedisPubSub } = require('graphql-redis-subscriptions');
const pubsub = new RedisPubSub();

const typeDefs = `
  type Query {
    _: Boolean
  }
  type Subscription {
    messageSent: String
  }
`;

const resolvers = {
  Subscription: {
    messageSent: {
      subscribe: () => pubsub.asyncIterator(['MESSAGE_SENT'])
    }
  }
};

// Somewhere in your code, you can publish an event
pubsub.publish('MESSAGE_SENT', { messageSent: 'Hello, world!' });

Custom Redis Client

This code demonstrates how to use a custom Redis client with graphql-redis-subscriptions. It uses the ioredis package to create custom Redis clients for the publisher and subscriber, allowing for more control over the Redis connection settings.

const Redis = require('ioredis');
const { RedisPubSub } = require('graphql-redis-subscriptions');

const options = {
  host: 'localhost',
  port: 6379,
  retryStrategy: times => {
    return Math.min(times * 50, 2000);
  }
};

const pubsub = new RedisPubSub({
  publisher: new Redis(options),
  subscriber: new Redis(options)
});

Filtering Subscriptions

This code demonstrates how to filter subscriptions using the withFilter function from graphql-subscriptions. It allows you to filter events based on custom logic, such as only sending events to specific users.

const { withFilter } = require('graphql-subscriptions');
const { RedisPubSub } = require('graphql-redis-subscriptions');
const pubsub = new RedisPubSub();

const typeDefs = `
  type Query {
    _: Boolean
  }
  type Subscription {
    messageSent(userId: ID): String
  }
`;

const resolvers = {
  Subscription: {
    messageSent: {
      subscribe: withFilter(
        () => pubsub.asyncIterator('MESSAGE_SENT'),
        (payload, variables) => {
          return payload.userId === variables.userId;
        }
      )
    }
  }
};

// Somewhere in your code, you can publish an event
pubsub.publish('MESSAGE_SENT', { messageSent: 'Hello, user!', userId: '123' });

Other packages similar to graphql-redis-subscriptions

Keywords

FAQs

Package last updated on 13 Dec 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