Skip to main content

Add messages to services

View as Markdown

A service in EventCatalog can receive (consume) or send (produce) messages (commands, events and queries).

Example

Producing and consuming messages from your service

To add messages to your service you first have to define your messages, if you don't have any messages you can create one.

To add messages to a service you need to define them in either the sends or receives array within your service frontmatter API.

/services/Orders/index.mdx (example)
---
id: OrderService
... # other service frontmatter
receives:
# id of the message this service receives (consumes)
- id: PaymentProcessed
# (optional) The version of the message you want to add.
# Supports semver matching (e.g ^1.0.1, 1.x.x).
# But if no version is given the latest version of the message will be used.
version: 0.0.1
sends:
# id of the message this service sends (produces)
- id: OrderProcessed
version: 2.x.x
---

<!-- Markdown contents... -->

The power of versioning

When you define your messages for your service you can define the version of them too. This can be powerful if you have multiple versions of your events, commands or queries. Example could be an API that you are consuming, maybe you are consuming an old version of this API you can specify that.

Routing messages through channels

Messages may also travel through channels (e.g message brokers, queues, buses).

To specify a channel you need to use the to and from fields in your service frontmatter.

This example shows :

  • the OrderService sending an OrderPlaced message to the orders.events channel.
  • the OrderService consuming the a PaymentProcessed message from the payments.events channel.
/services/Orders/index.mdx (example)
---
id: OrderService
... # other service frontmatter

# Service sends a message called OrderPlaced
# This message is published to the orders.events channel (e.g broker)
sends:
- id: OrderPlaced
to:
# The id of the channel (e.g queue)
- id: orders.events

# Service consumes a message called PaymentProcessed
# This message is consumed from the payments.events channel (e.g queue)
receives:
- id: PaymentProcessed
from:
# The id of the channel (e.g bus)
- id: payments.events
---

You can read more about routing messages through channels in the routing messages through channels guide.