01

Sample Code

pubsub-demo.py
from psp.platform.eventbus import InMemoryEventBus
from psp.platform.contracts import EventEnvelope

# Create event bus
event_bus = InMemoryEventBus()

# Subscribe to specific event type
def on_task_created(event: EventEnvelope) -> None:
    print(f"Task created: {event.payload['task_id']}")

event_bus.subscribe("TaskCreated", on_task_created)

# Subscribe to ALL events with wildcard
def on_any_event(event: EventEnvelope) -> None:
    print(f"Event: {event.payload.get('type')}")

event_bus.subscribe("*", on_any_event)

# Publish events
event_bus.publish(EventEnvelope(
    payload={"type": "TaskCreated", "task_id": "123"},
    aggregate_id="123",
    aggregate_type="Task",
))
# Output: "Task created: 123" (specific handler)
#         "Event: TaskCreated" (wildcard handler)

event_bus.publish(EventEnvelope(
    payload={"type": "TaskCompleted", "task_id": "123"},
    aggregate_id="123",
    aggregate_type="Task",
))
# Output: "Event: TaskCompleted" (only wildcard handler)