Publishing Events
Class-level publish
The most common way to publish:
ruby
Order::Placed.publish(order_id: 1, total: 99.99) # async (default)
Order::Placed.publish(order_id: 1, total: 99.99, sync: true) # syncInstance-level publish
Create the event first, then publish:
ruby
event = Order::Placed.new(order_id: 1, total: 99.99)
event.publish # async
event.publish(sync: true) # syncUseful when you need the event object (e.g., for tracing).
Sync vs async
Async (default): Each handler is dispatched as an ActiveJob. Handlers run independently — one failure doesn't affect others.
Sync: Handlers called inline in the current thread. Useful for tests and when you need immediate execution.
Caused by
Link an event to its cause:
ruby
order_event = Order::Placed.new(order_id: 1, total: 99.99)
Shipment::Reserved.publish(order_id: 1, caused_by: order_event)The child event's caused_by_id is set to the parent's id, and they share the same trace_id. See Tracing for details.
What happens on publish
- Check suppression — skip if suppressed
- Persist to
event_storeif configured (failure doesn't halt) - Find subscribed handlers
- Dispatch each handler (async or sync)