Adds a new message interceptor to the bus.
Message interceptors allow inspecting, modifying, or vetoing messages before they are dispatched to subscribed handlers.
Interceptors are invoked in reverse order of registration: the most recently added interceptor will wrap all previously added ones.
The interceptor to add.
Adds a message listener that will be notified of every message published on this message bus, regardless of topic.
Listeners are invoked before any topic-specific subscribers. This allows observing messages even if no subscriber for a topic exists.
The listener to add.
Creates a new child bus linked to this one for hierarchical broadcasting.
Messages with children broadcast direction will be propagated to it.
Optionaloptions: Partial<ChildMessageBusOptions>Disposes the message bus, all its child buses, and all active subscriptions.
After disposal, neither this bus nor any child buses can be used for publishing or subscribing.
Publishes a new message without any associated data to the specified topic.
The topic to publish the message to.
Asynchronously publishes a new message without any associated data to the specified topic and waits for all subscribed handlers to complete.
The returned promise resolves once all subscribed handlers have completed:
unicast topics, it resolves to the single handler's result.multicast topics, it resolves to an array of all handler results.If one or more handlers throw, the promise is rejected:
AggregateError containing all errors if multiple handlers failed.The topic to publish the message to.
A promise that resolves with the handler result(s), or rejects if any handler throws.
Asynchronously publishes a new message with associated data to the specified topic and waits for all subscribed handlers to complete.
The returned promise resolves once all subscribed handlers have completed:
unicast topics, it resolves to the single handler's result.multicast topics, it resolves to an array of all handler results.If one or more handlers throw, the promise is rejected:
AggregateError containing all errors if multiple handlers failed.The topic to publish the message to.
The data payload to send with the message.
A promise that resolves with the handler result(s), or rejects if any handler throws.
Removes a previously added message interceptor.
The interceptor to remove.
Removes a previously added message listener.
The listener to remove.
Creates a lazily-initialized subscription to the specified topic that is also an AsyncIterableIterator.
This allows consuming published messages using the for await ... of syntax.
If an async iteration completes or ends early (e.g., via break, return, or an error),
the subscription is automatically disposed.
The subscription is created lazily: the first call to next() or single()
triggers the underlying registration. If the consumer never starts an iteration
or never awaits a message, no subscription is created.
Subscribes to the specified topic with a callback.
The subscription is established immediately, and you can call Subscription.dispose to unsubscribe.
Creates subscriptions for the given instance using the topic metadata defined
on its class's methods via @Topic()-decorated parameters.
Each discovered method is bound to the instance and invoked whenever a message is published to its associated topic. Subscriptions are cleaned up automatically when the instance is garbage-collected, or when the instance is passed to unsubscribeInstance.
An instance whose class contains @Topic()-decorated methods.
class CommandProcessor {
// The Subscription parameter is optional.
// If present, it must immediately follow the decorated parameter.
onCommand(@CommandTopic() command: string, subscription: Subscription): void {
if (command === "shutdown") {
// ...
subscription.dispose();
}
}
}
// The onCommand method will be registered as a CommandTopic handler
const processor = new CommandProcessor();
messageBus.subscribeInstance(processor);
Subscribes once to the specified topic, returning a promise that resolves with the next published message.
The subscription will be automatically disposed after receiving the message. This allows awaiting a single message without manual subscription management.
Subscribes once to the specified topic with a callback.
The callback is invoked with the next message, after which the subscription is disposed.
Disposes all subscriptions to topics previously registered for the given instance via subscribeInstance. Any method handler bound to the instance will no longer be invoked when messages are published to its associated topic.
An instance whose subscriptions should be disposed.
Sets the maximum number of messages to receive for the next subscription.
When the specified limit is reached, the subscription is automatically disposed.
The maximum number of messages to receive.
Sets the priority for the next subscription.
Higher priority (lower number) subscriptions are notified before lower priority
(higher value) ones. The default priority value is 1.
A priority value, where a lower number means higher priority.
The message bus API.