One mod publishes what it knows and another renders it, so a data source and a display can be written by different people. A channel is an object per topic; both sides get it by name.
| Scope | Installs |
|---|---|
mods.messaging | perch.mods.channel(name) |
Both sides need the scope: a mod that does not declare it neither publishes nor hears anything. Treat a received payload as untrusted input, the way you would an HTTP response.
perch.mods.channel
perch.mods.channel<T>(name: string): channel_t<T>
The channel for that topic. name is 1 to 64 characters (else perch.mods.channel: name must be 1-64 characters); calling it twice with the same name returns the same object.
| Member | Meaning |
|---|---|
channel:publish(payload) | broadcast to every other mod listening on the topic. A publisher never hears its own message |
channel.on_message | signal_t<T, string>; fires (payload, from), where from is the publishing mod’s id |
--!strict
type prices_t = { btc: number }
local prices: channel_t<prices_t> = perch.mods.channel("prices")
prices:publish({ btc = 61234 })
prices.on_message:connect(function(p: prices_t, from: string)
w:push({ btc = perch.text.number(p.btc) })
end)
Types
export type channel_t<T> = {
on_message: signal_t<T, string>,
publish: (self: channel_t<T>, payload: T) -> (),
}
T is the payload shape you agree on with the other side.