GraphQL Subscriptions with Redis PubSub

GraphQL Subscriptions with Redis PubSub

Β·

3 min read

In the last article, Realtime GraphQL Subscriptions with Node.js, we learned to create a real-time application using GraphQL subscriptions using In-built Pubsub mechanism by graphql-yoga

In this article, we would be combining the graphql-subscriptions with the Redis publish/subscribe mechanism which is a recommended way to make our production applications more scalable.

Pre-requisites

Graphql Redis Subscriptions

Graphql Redis Subscription allows you to connect your subscriptions manager to a Redis PubSub mechanism to support multiple subscription-manager instances.

Let's Understand Redis PubSub Mechanism

Let's see the Redis way in which we can publish a channel and subscribe to the channel to listen to all the realtime messages.

  • Publish messages via a channel - Create a channel and publish a new message

1.png

  • Subscribe to the channel to listen to all the incoming messages

2.png

  • Now, send the publish command again and check the output for the subscribed channel in another terminal

3.png

  • Output - Once subscribe the channel, you can listen to all the incoming messages publish to the channel

4.png

Integration with Graphql Redis Subscriptions

Kindly check out the codebase from our GitHub repo for further configuration of Redis pubsub mechanism

  • Install dependencies
npm install --save graphql-redis-subscriptions ioredis
  • graphql-redis-subscriptions - This package implements the PubSubEngine interface and can be easily integrated with Redis
  • ioredis - It's a redis client for Node.js used by Ali-Baba

  • Insert below code to configure redis and graphql-redis-subscriptions

import { RedisPubSub } from 'graphql-redis-subscriptions';
import Redis from 'ioredis';


const options = {
    host: process.env.REDIS_HOST || '127.0.0.1',
    port: process.env.REDIS_PORT ? process.env.REDIS_PORT : '6379',
    retryStrategy: times => {
        // reconnect after
        return Math.min(times * 50, 2000);
    }
};

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

Demo

  • Run our application
npm run build
npm run start
  • Visit localhost

  • Subscribe to the post channel - from the graphql playground as well as redis-cli

Graphql Playground

5.png

redis-cli

6.png

  • Now, let's create and delete the post from the playground, and check them out in both the graphql playground and redis-cli

Create Post Mutation

7.png

Post Subscription with Payload

8.png

Verify the same on redis server

10.png

AND WE'RE DONE!

Conclusion

CongratulationπŸ₯³ , If you made till here. Your entire application is now using Redis pubsub mechanism and it would make your application much more scalable.

I have created a repository of this app on my Github, please feel free to fork the code and try to run all the commands/code.

If you liked it please leave some love to show your support. Also, leave your responses below and reach out to me if you face any issues.

Did you find this article valuable?

Support Jay Desai by becoming a sponsor. Any amount is appreciated!

Β