Publish/subscribe message bus for decoupled communication
Enables loose coupling between components through topic-based messaging. Any component can post messages to topics, and any component can subscribe to receive them. Messages are delivered synchronously during the next scheduler update. See the Message Bus tab for available system topics.
| Parameter | Description |
|---|---|
| topic | Topic string to check |
if ez.bus.has_subscribers("debug/memory") then
ez.bus.post("debug/memory", {heap = ez.system.get_free_heap()})
end
local pending = ez.bus.pending_count()
if pending > 10 then
print("Warning: message queue backing up")
end
| Parameter | Description |
|---|---|
| topic | Topic string to post to |
| data | Message data (string or table) |
-- Post a string message
ez.bus.post("status/update", "connected")
-- Post a table with structured data
ez.bus.post("chat/message", {sender = "Alice", text = "Hello!"})
| Parameter | Description |
|---|---|
| topic | Topic string to subscribe to |
| callback | Function(topic, data) called when message received |
local sub_id = ez.bus.subscribe("mesh/message", function(topic, data)
print("Received:", data.text, "from", data.sender)
end)
| Parameter | Description |
|---|---|
| subscription_id | ID returned from subscribe() |
function MyScreen:on_exit()
if self.sub_id then
ez.bus.unsubscribe(self.sub_id)
end
end