zephyr.bus

Send JSON-like messages between installed modules. Bus operations are not permission-gated.

4 functions

emit

zephyr.bus.emit(target, topic, payload)

Asynchronously send a message to another module.

If target is not installed, the host logs a warning and drops the message.

Parameters

targetstring

Module id to deliver the message to.

topicstring

Message topic the target subscribes to.

payloadany

JSON-like payload value.

Returns

nil

Nothing.

Example

lua
zephyr.bus.emit("tickets", "ticket.closed", { ticket_id = "abc" })

on

zephyr.bus.on(topic, handler)

Register a handler for messages delivered to this module on a topic.

Parameters

topicstring

Topic to subscribe to.

handlerfunction

Called as function(from, topic, payload) when a message arrives.

Returns

nil

Nothing.

Example

lua
zephyr.bus.on("ticket.closed", function(from, topic, payload)
  zephyr.log("info", "ticket closed by " .. from)
end)

request

zephyr.bus.request(target, topic, payload)

Send a request to another module and await its reply.

Suspends until the target module's bus.respond handler for topic returns. Returns nil if the target has no responder for that topic. Raises if the target is not installed, or if a module requests itself.

Parameters

targetstring

Module id to send the request to.

topicstring

Request topic the target responds to.

payloadany

JSON-like payload value.

Returns

any

The value returned by the target's responder, or nil.

Example

lua
-- Ask the Embed Workshop to render a saved embed, then post it with role buttons.
local embed = zephyr.bus.request("workshop", "workshop.get_embed", { embed_id = id })

respond

zephyr.bus.respond(topic, handler)

Register a responder for bus.request calls to this module on a topic.

Parameters

topicstring

Request topic to answer.

handlerfunction

Called as function(from, payload); its return value is sent back to the requester.

Returns

nil

Nothing.

Example

lua
zephyr.bus.respond("workshop.get_embed", function(from, payload)
  return build_embed(load_embed(payload.embed_id))
end)