Skip to content

Message Queue

Publish and consume messages from distributed queues. Supports multiple backends including RabbitMQ and other AMQP-compatible brokers.

For queue configuration, see Queue.

local queue = require("queue")

Send messages to a queue by ID:

local ok, err = queue.publish("app:tasks", {
action = "send_email",
user_id = 456,
template = "welcome"
})
if err then
return nil, err
end
ParameterTypeDescription
queue_idstringQueue identifier (format: “namespace:name”)
dataanyMessage data (tables, strings, numbers, booleans)
headerstableOptional message headers

Returns: boolean, error

Headers enable routing, priority, and tracing:

queue.publish("app:notifications", {
type = "order_shipped",
order_id = order.id
}, {
priority = "high",
correlation_id = request_id
})

Within a queue consumer, access the current message:

local msg, err = queue.message()
if err then
return nil, err
end
local msg_id = msg:id()
local priority = msg:header("priority")
local all_headers = msg:headers()

Returns: Message, error

Only available when processing queue messages in consumer context.

MethodReturnsDescription
id()string, errorUnique message identifier
header(key)any, errorSingle header value (nil if missing)
headers()table, errorAll message headers
ack()boolean, errorAcknowledge processing (single-shot)
nack()boolean, errorSignal failure for redelivery or dead-letter (single-shot)

The runtime auto-acks on handler success and auto-nacks on handler error. Call ack/nack only to settle early.

local stats, err = queue.info("app:tasks")
-- stats may contain: message_count, consumer_count, ready (driver-dependent)

Returns: table, error

A queue.consumer entry binds a queue to a handler function (referenced by func). The handler receives the message payload directly:

entries:
- kind: queue.consumer
id: email_worker
queue: app:emails
func: app:email_handler
-- app:email_handler
function handle_email(payload)
local msg = queue.message()
logger:info("Processing", {
message_id = msg:id(),
to = payload.to
})
local ok, err = email.send(payload.to, payload.template, payload.data)
if err then
return nil, err -- Message will be requeued or dead-lettered
end
end

Queue operations are subject to security policy evaluation.

ActionResourceDescription
queue.publish-General permission to publish messages
queue.publish.queueQueue IDPublish to specific queue

Both permissions are checked: first the general permission, then the queue-specific one.

ConditionKindRetryable
Queue ID emptyerrors.INVALIDno
Message data emptyerrors.INVALIDno
No delivery contexterrors.INVALIDno
Publish not allowederrors.INVALIDno
Publish failederrors.INTERNALno

See Error Handling for working with errors.