Skip to main content

@baeta/subscriptions-pubsub

Interfaces

PubSubEngineV2

Properties

PropertyType

asyncIterator

<T>(triggers: string | string[], ...rest: any[]) => AsyncIterator<T, any, any>

publish

(triggerName: string, payload: any, ...rest: any[]) => Promise<void>

subscribe

(triggerName: string, onMessage: (message) => void | Promise<void>, ...rest: any[]) => Promise<number>

unsubscribe

(subId: number, ...rest: any[]) => void


PubSubEngineV3

Properties

PropertyType

asyncIterableIterator

<T>(triggers: string | readonly string[], ...rest: any[]) => AsyncIterableIterator<T, any, any>

publish

(triggerName: string, payload: any, ...rest: any[]) => Promise<void>

subscribe

(triggerName: string, onMessage: (message) => void | Promise<void>, ...rest: any[]) => Promise<number>

unsubscribe

(subId: number, ...rest: any[]) => void


TypedPubSubOptions

Configuration options for TypedPubSub

Properties

PropertyTypeDescription

prefix?

string

Optional prefix for channel names

Type Aliases

TypedPubSub<Engine, Map>

TypedPubSub<Engine, Map>: Engine extends PubSubEngineV3 ? TypedPubSubV3<Engine, Map> : Engine extends PubSubEngineV2 ? TypedPubSubV2<Engine, Map> : never

Type Parameters

Type Parameter

Engine extends PubSubEngineV2 | PubSubEngineV3

Map extends Record<string, any>

Functions

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:",
},
);