AboutBlogContact
BackendMarch 25, 2018 2 min read 25

GraphQL Subscriptions: Real-time Data with Redis PubSub (2018)

AunimedaAunimeda
📋 Table of Contents

GraphQL Subscriptions with Redis

By 2018, GraphQL has conquered the API world. But until recently, we still relied on traditional WebSockets or polling for real-time data. GraphQL Subscriptions have finally standardized how we push data from the server to the client.

The Subscription Schema

In your schema, you define subscriptions just like queries:

type Subscription {
  messageAdded(roomId: ID!): Message
}

The Backend: Apollo Server and Redis

For a single server, the default PubSub implementation in memory works fine. But in production, you're likely running multiple instances of your server. This is where Redis PubSub becomes essential.

const { RedisPubSub } = require('graphql-redis-subscriptions');
const pubsub = new RedisPubSub({
  connection: {
    host: REDIS_DOMAIN_NAME,
    port: PORT_NUMBER
  }
});

const resolvers = {
  Subscription: {
    messageAdded: {
      subscribe: (parent, { roomId }) => {
        return pubsub.asyncIterator(`MESSAGE_ADDED_${roomId}`);
      },
    },
  },
  Mutation: {
    addMessage: async (parent, { roomId, text }) => {
      const newMessage = await saveMessage(roomId, text);
      pubsub.publish(`MESSAGE_ADDED_${roomId}`, { messageAdded: newMessage });
      return newMessage;
    }
  }
};

The Client: Subscription Transport WS

On the client, Apollo uses the subscriptions-transport-ws library to maintain a persistent connection.

const client = new ApolloClient({
  link: split(
    ({ query }) => {
      const definition = getMainDefinition(query);
      return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
    },
    wsLink,
    httpLink
  ),
  cache: new InMemoryCache()
});

With this setup, our frontend can react to database changes in milliseconds without ever having to refresh. It's the "holy grail" of data synchronization in 2018.

Read Also

WebRTC: Scaling P2P Mesh Networking for Real-time Video (2019)aunimeda
Backend

WebRTC: Scaling P2P Mesh Networking for Real-time Video (2019)

Is it possible to build a video chat app without an expensive media server? In 2019, we're exploring the limits of WebRTC mesh networking.

Phoenix Channels: Real-time Web Without the Scalability Headache (2016)aunimeda
Backend

Phoenix Channels: Real-time Web Without the Scalability Headache (2016)

Is Node.js the king of real-time? Not anymore. In 2016, Elixir and Phoenix are showing us how to handle 2 million connections on a single node.

GraphQL vs REST API in 2026: A Practical Guide to Choosing the Right Approachaunimeda
Backend

GraphQL vs REST API in 2026: A Practical Guide to Choosing the Right Approach

GraphQL has been 'the future of APIs' for almost a decade. REST has been 'dying' for just as long. Both are still widely used in 2026 — because they solve different problems. Here's a practical framework for choosing, with real tradeoffs from production systems.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles