Skip to main content

Overview

The Messaging Service provides a type-safe mechanism for sending and receiving messages across different components of the application. It supports both legacy string-based commands and modern type-safe command definitions.

MessageSender

The abstract base class for sending messages throughout the application.

Interface

Methods

send() (Type-Safe)

Sends a message in a type-safe manner. The command definition ensures the payload matches the expected type. Parameters:
  • commandDefinition (CommandDefinition<T>): The command definition that specifies the message type and payload structure
  • payload (T): The message payload, which must match the type defined in the command definition
Returns: void Example:

send() (Legacy)

Sends a message using a string-based command (legacy method). Parameters:
  • command (string): The command identifier
  • payload (Record<string, unknown>, optional): The message payload
Returns: void Example:
Consider using CommandDefinition instead of string-based commands to get compilation errors when defining an incompatible payload.

Static Methods

combine()

Combines multiple message senders into a single sender that relays messages to all of them. Parameters:
  • messageSenders (...MessageSender[]): The message senders to combine
Returns: MessageSender - A composite message sender Example:

Static Properties

EMPTY

A message sender that sends to nowhere. Useful for testing or disabled states. Example:

MessageListener

A class for listening to messages coming through the application.

Interface

Constructor

Parameters:
  • messageStream (Observable<Message<Record<string, unknown>>>): The underlying observable stream of messages

Properties

allMessages$

A stream of all messages sent through the application. Does not contain type information for message properties. Example:

Methods

messages$<T>()

Creates an observable stream filtered to a specific command with proper typing. Parameters:
  • commandDefinition (CommandDefinition<T>): The command definition to filter for
Returns: Observable<T> - Stream of messages matching the command definition Example:
Be careful using this method unless all messages are sent through MessageSender.send with proper command definitions. Otherwise, you should have lower confidence in the message payload being the expected type.

Static Properties

EMPTY

A message listener that never emits any messages and immediately completes.

Types

CommandDefinition

Defines information about a message type, providing type-safe messaging alongside MessageSender and MessageListener. Parameters:
  • command (string): The command identifier
Example:

Message

Represents a message with a command identifier and typed payload.

Usage Examples

Type-Safe Messaging

Legacy String-Based Messaging

Combining Message Senders

Using Empty Implementations

Best Practices

Consider NOT using messaging at all if you can. State Providers offer an observable stream of data that is persisted. This can serve use cases that might have previously used messages to notify of settings changes or vault data changes, and those observables should be preferred over messaging.

When to Use Messaging

  • Cross-component event notifications that don’t require persistence
  • Triggering actions in response to user events
  • Broadcasting system-wide state changes

When NOT to Use Messaging

  • Persisting data (use State Providers instead)
  • Notifying about data changes (use State Provider observables)
  • Sharing configuration (use State Providers)