Skip to content

Class: RedisPubSub

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:43

Redis-backed implementation of the PubSubEngine contract.

Uses two separate ioredis connections — one for publishing (PUBLISH) and one dedicated to blocking subscribe commands (SUBSCRIBE / PSUBSCRIBE). This separation is required because a Redis client in subscribe mode can only issue subscribe/unsubscribe commands.

Construct via GraphQLPubSubModule.forRoot or GraphQLPubSubModule.forRootAsync in module-based apps.

Example

typescript
const pubsub = new RedisPubSub({ connection: { host: "localhost", port: 6379 } });

// Publish
await pubsub.publish("ORDER_CREATED", { id: 1 });

// Subscribe
const subId = await pubsub.subscribe("ORDER_CREATED", (payload) => {
  console.log(payload);
});

// Later…
pubsub.unsubscribe(subId);
await pubsub.close();

Implements

Accessors

channelCount

Get Signature

ts
get channelCount(): number;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:384

Number of distinct Redis channels (after triggerTransform) that this instance is currently subscribed to.

Returns

number


subscriptionCount

Get Signature

ts
get subscriptionCount(): number;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:376

Number of active per-iterator subscriptions currently held in memory.

Useful for exporting as a metric — steady growth here when client churn is normal is a sign that subscription iterators aren't being returned on disconnect (typically a dirty-WebSocket-close issue).

Returns

number

Constructors

Constructor

ts
new RedisPubSub(options?): RedisPubSub;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:71

Parameters

ParameterType
optionsRedisPubSubOptions

Returns

RedisPubSub

Methods

asyncIterator()

ts
asyncIterator<T>(triggers, options?): AsyncIterator<T>;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:346

Creates an AsyncIterator over the given triggers for use in GraphQL subscription resolvers.

Type Parameters

Type Parameter
T

Parameters

ParameterType
triggersstring | string[]
options?AsyncIteratorOptions

Returns

AsyncIterator<T>

Example

typescript
// In a GraphQL resolver:
subscribe() {
  return pubsub.asyncIterator<OrderCreatedEvent>("ORDER_CREATED");
}

Implementation of

PubSubEngine.asyncIterator


close()

ts
close(): Promise<void>;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:417

Gracefully closes both Redis connections.

Call this during application shutdown to allow open sockets to drain.

Returns

Promise<void>


debug()

ts
debug(): {
  channelCount: number;
  channels: {
     subscribers: number;
     trigger: string;
  }[];
  subscriptionCount: number;
};

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:396

Snapshot of the subscription state for observability.

The returned structure is a copy — mutating it does not affect the internal maps. Intended for /health endpoints, metric collectors, and regression tests for leak detection. Preferable to reaching into subscriptionMap via as unknown as { subscriptionMap }.

Returns

ts
{
  channelCount: number;
  channels: {
     subscribers: number;
     trigger: string;
  }[];
  subscriptionCount: number;
}
NameTypeDefined in
channelCountnumberpackages/graphql-pubsub/src/redis-pubsub.ts:398
channels{ subscribers: number; trigger: string; }[]packages/graphql-pubsub/src/redis-pubsub.ts:399
subscriptionCountnumberpackages/graphql-pubsub/src/redis-pubsub.ts:397

getPublisher()

ts
getPublisher(): Redis;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:365

Returns the underlying publisher Redis client.

Returns

Redis


getSubscriber()

ts
getSubscriber(): Redis;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:360

Returns the underlying subscriber Redis client.

Returns

Redis

Warning

Do not issue regular Redis commands on this client — it is in subscribe mode and only accepts (P)SUBSCRIBE / (P)UNSUBSCRIBE.


publish()

ts
publish(trigger, payload): Promise<void>;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:142

Publishes payload to the Redis channel derived from trigger.

The payload is serialised with the custom RedisPubSubOptions.serializer when provided, otherwise JSON.stringify.

Parameters

ParameterType
triggerstring
payloadunknown

Returns

Promise<void>

Implementation of

PubSubEngine.publish


subscribe()

ts
subscribe<T>(
   trigger, 
   onMessage, 
   options?
): Promise<number>;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:160

Subscribes to trigger and invokes onMessage for every incoming message.

If another subscriber already holds a subscription for the same resolved channel, the existing Redis subscription is reused — no additional SUBSCRIBE command is issued.

Type Parameters

Type ParameterDefault type
Tunknown

Parameters

ParameterType
triggerstring
onMessageMessageHandler<T>
optionsSubscriptionOptions

Returns

Promise<number>

A numeric subscription ID used with unsubscribe.

Implementation of

PubSubEngine.subscribe


unsubscribe()

ts
unsubscribe(subId): void;

Defined in: packages/graphql-pubsub/src/redis-pubsub.ts:298

Cancels the subscription identified by subId.

When the cancelled subscription was the last one for its Redis channel, the corresponding UNSUBSCRIBE / PUNSUBSCRIBE command is issued.

Parameters

ParameterType
subIdnumber

Returns

void

Throws

If subId is not a known subscription.

Implementation of

PubSubEngine.unsubscribe

Released under the MIT License.