Message Bus
๐ฌ MessageBus
The MessageBus
It is the core interface used to dispatch messages within your microservice architecture. It acts as the gateway between your application and the configured messaging channels, enabling seamless communication between services.
Whether you're calling from a controller, service, or any provider, the MessageBus
gives you a powerful and type-safe way to route and send messages through your event-driven system.
๐ What Is the MessageBus?
The MessageBus
is injected via the @MessageBus('bus.name')
decorator and gives you access to methods that allow you to:
Send messages to specific handlers via routing
Send raw or structured messages
Target a specific channel (e.g., in-memory, AMQP)
Trigger synchronous or asynchronous processing
๐ Injecting the MessageBus
When configuring your messaging module, you define named buses. To use a bus, inject it using its name.
Example
๐งฉ RoutingMessage Structure
To route a message to the correct handler, use the RoutingMessage
class:
Example:
๐ก Bus-to-Channel Mapping
In your configuration, each bus can be connected to multiple channels:
โ A message dispatched from a bus is passed to each of its configured channels.
๐ One Bus, Many Channels
Each bus can dispatch to multiple channels, and you can even configure different channels (e.g., RabbitMQ + InMemory) to receive the same message for hybrid systems.
๐ Benefits of MessageBus
โ Type-safe
Dispatch class-based messages with strict typing
๐ Pluggable
Supports multiple channels per bus
๐ก Scalable
Easily add more channels or handlers without changing your core logic
๐งช Testable
Easily mock or simulate dispatches in testing environments
Last updated