Message Broker

Message Replaying

Message replaying allows new subscribers to receive the latest ā€œnā€ messages published on a channel. The replayCacheSize setting controls the maximum number of messages stored for new subscribers. This configuration can only be provided when a channel is created.

import { messageBroker } from '@morgan-stanley/message-broker';

interface IChannels {
    channel: string;
}

const broker = messageBroker<IChannels>();

broker.create('channel', { replayCacheSize: 2 }).publish('Jupiter');

broker.get('channel').subscribe((message) => {
    console.log(message.data);
});

// Logs: Jupiter

Channel disposal

The replay cache belongs to the channel that created it. Calling dispose removes the channel, its configuration, and the messages stored in its replay cache.

const broker = messageBroker<IChannels>();

broker.create('channel', { replayCacheSize: 2 }).publish('Jupiter');

broker.dispose('channel');

broker.get('channel').subscribe((message) => {
    console.log(message.data);
});

// Nothing is logged because the channel and its cached messages were disposed.

After disposal, a channel with the same name can be created again, but it starts with a new and empty replay cache.

Creating an existing channel with the same name but a different configuration will throw an error.