Typed Channels¶
Channels can enforce a type constraint on published messages. When set, every message must pass an is_a? check against the specified class.
Setting a Type¶
Or via MessageBus:
Type Enforcement¶
The error message includes both the expected and actual types:
Subclasses¶
The check uses is_a?, so subclasses pass:
class PriorityOrder < Order; end
channel = TypedBus::Channel.new(:orders, type: Order, timeout: 5)
Async do
channel.publish(PriorityOrder.new) # OK — PriorityOrder is_a? Order
end
Untyped Channels¶
Omit type: (or pass nil) to accept any message:
When to Use Types¶
Type constraints are useful when:
- Multiple publishers share a channel and you want compile-time-like safety
- Messages carry structured data (Structs, Data classes) and you want to catch serialization mistakes early
- You want to document the channel's contract in code
Note
The type: parameter is per-channel only. It is not part of the configuration cascade — there is no meaningful "default type."