Kit
Functions

demultiplexDataPublisher

demultiplexDataPublisher<TDataPublisher, TChannelName>(publisher, sourceChannelName, messageTransformer): DataPublisher

Given a channel that carries messages for multiple subscribers on a single channel name, this function returns a new DataPublisher that splits them into multiple channel names.

Type Parameters

Type Parameter
TDataPublisher extends DataPublisher<Record<string, unknown>>
TChannelName extends string

Parameters

ParameterTypeDescription
publisherTDataPublisher-
sourceChannelNameTChannelName-
messageTransformer(message) => void | [string, unknown]A function that receives the message as the first argument, and returns a tuple of the derived channel name and the message.

Returns

DataPublisher

Example

Imagine a channel that carries multiple notifications whose destination is contained within the message itself.

const demuxedDataPublisher = demultiplexDataPublisher(channel, 'message', message => {
    const destinationChannelName = `notification-for:${message.subscriberId}`;
    return [destinationChannelName, message];
});

Now you can subscribe to only the messages you are interested in, without having to subscribe to the entire 'message' channel and filter out the messages that are not for you.

demuxedDataPublisher.on(
    'notification-for:123',
    message => {
        console.log('Got a message for subscriber 123', message);
    },
    { signal: AbortSignal.timeout(5_000) },
);

On this page