Skip to content

Event Bus

Publish and subscribe to events for observability — monitoring runtime and application activity and reacting to it.

Use the event bus for observation only: monitoring, logging, metrics, and reactive side effects. It is a best-effort publish/subscribe channel, not a reliable transport — do not build business logic on it or depend on it for guaranteed delivery. For business-critical messaging use process messaging (`process.send`), channels, or the [message queue](lua/storage/queue.md).
local events = require("events")

Subscribe to events from the event bus:

-- Subscribe to all order events
local sub, err = events.subscribe("orders.*")
if err then
return nil, err
end
-- Subscribe to specific event kind
local sub = events.subscribe("users", "user.created")
-- Subscribe to all events from a system
local sub = events.subscribe("payments")
-- Process events
local ch = sub:channel()
while true do
local evt, ok = ch:receive()
if not ok then break end
logger:info("Received event", {
system = evt.system,
kind = evt.kind,
path = evt.path
})
handle_event(evt)
end
ParameterTypeDescription
systemstringSystem pattern (supports wildcards like “test.*“)
kindstringEvent kind filter (optional)

Returns: Subscription, error

Send an event to the event bus:

-- Send order created event
local ok, err = events.send("orders", "order.created", "/orders/123", {
order_id = "123",
customer_id = "456",
total = 99.99
})
if err then
return nil, err
end
-- Send user event
events.send("users", "user.registered", "/users/" .. user.id, {
user_id = user.id,
email = user.email,
created_at = time.now():format("2006-01-02T15:04:05Z07:00")
})
-- Send payment event
events.send("payments", "payment.completed", "/payments/" .. payment.id, {
payment_id = payment.id,
order_id = payment.order_id,
amount = payment.amount,
method = payment.method
})
-- Send without data
events.send("system", "heartbeat", "/health")
ParameterTypeDescription
systemstringSystem identifier
kindstringEvent kind/type
pathstringEvent path for routing
dataanyEvent payload (optional)

Returns: boolean, error

Get the channel for receiving events:

local ch = sub:channel()
local evt, ok = ch:receive()
if ok then
print("System:", evt.system)
print("Kind:", evt.kind)
print("Path:", evt.path)
print("Data:", json.encode(evt.data))
end

Event fields: system, kind, path, data

Unsubscribe and close the channel:

sub:close()
ActionResourceDescription
events.subscribesystemSubscribe to events from a system
events.sendsystemSend events to a system
ConditionKindRetryable
Empty systemerrors.INVALIDno
Empty kinderrors.INVALIDno
Empty patherrors.INVALIDno
Policy deniederrors.INVALIDno

See Error Handling for working with errors.