Skip to content

loryth-messages — Consumer Guide

Audience: Consumers — developers using loryth-messages in their services.

A messaging library that facilitates communication between services.

Overview

The loryth-messages library provides a robust framework for implementing message-based communication between services. It supports various messaging patterns and integrates with message brokers like RabbitMQ.

Key Components

Base Message Classes

  • BaseMessage: A base class for all messages, providing common functionality for message serialization, validation, and metadata.
  • MessageType: An enumeration of message types (e.g., command, event, query).

Message Receipt Handling

  • MessageReceipt: A class that represents the receipt of a message, including status information.
  • MessageReceiptStatus: An enumeration of message receipt statuses (e.g., delivered, failed).

RabbitMQ Integration

The library includes comprehensive support for RabbitMQ:

  • PikaConsumer: A consumer implementation using the Pika library.
  • PikaProducer: A producer implementation using the Pika library.
  • PikaConnection: A connection manager for RabbitMQ.
  • PikaContainer: A container for managing RabbitMQ resources.
  • PikaStatus: Status information for RabbitMQ connections.
  • PikaManagement: Management utilities for RabbitMQ.

Protocol Definitions

  • Producer: A protocol for message producers.
  • Consumer: A protocol for message consumers.
  • Management: A protocol for message broker management.

Usage

To use the loryth-messages library, import the specific components you need:

from loryth.messages import BaseMessage, MessageType, MessageReceipt
from loryth.messages.pika import PikaProducer, PikaConsumer

# Example message definition
class UserCreatedMessage(BaseMessage):
    def __init__(self, user_id, name, email):
        super().__init__(
            message_type=MessageType.EVENT,
            payload={
                "user_id": user_id,
                "name": name,
                "email": email
            }
        )

# Example producer usage
producer = PikaProducer(connection_params={"host": "localhost"})
message = UserCreatedMessage("user-123", "John Doe", "john@example.com")
receipt = producer.publish(message, exchange="users", routing_key="user.created")

# Example consumer usage
def handle_user_created(message):
    user_id = message.payload["user_id"]
    name = message.payload["name"]
    print(f"User created: {name} ({user_id})")

consumer = PikaConsumer(connection_params={"host": "localhost"})
consumer.subscribe(
    queue="user_service",
    callback=handle_user_created,
    exchange="users",
    routing_key="user.created"
)
consumer.start_consuming()

Integration with Other Libraries

The loryth-messages library is designed to work seamlessly with other libraries in the Loryth ecosystem:

  • It uses loryth-common for common utilities and patterns.
  • It integrates with loryth-domain for publishing domain events.
  • It can be used with any service that needs to communicate with other services.