Skip to main content

createTypedPubSub()

createTypedPubSub<Engine, Map>(pubsub, options?): TypedPubSub<Engine, Map>

Creates a type-safe wrapper around a PubSub implementation.

This utility ensures that your subscription channels and their payloads are properly typed, helping catch potential errors at compile time.

Type Parameters

Type Parameter

Engine extends PubSubEngineV2 | PubSubEngineV3

Map extends Record<string, any>

Parameters

ParameterTypeDescription

pubsub

Engine

The PubSub engine instance (e.g., PubSub, RedisPubSub)

options?

TypedPubSubOptions

Configuration options

Returns

TypedPubSub<Engine, Map>

Example

// Define your event map
type PubSubMap = {
"user-updated": User;
[c: `user-updated-${string}`]: User;
};

// Create typed PubSub instance
const pubsub = createTypedPubSub<PubSub, PubSubMap>(new PubSub());

// Usage with Redis
const pubsub = createTypedPubSub<RedisPubSub, PubSubMap>(
new RedisPubSub({
publisher: new Redis(options),
subscriber: new Redis(options),
}),
{
prefix: "feature-1:",
},
);