Quick Start¶
Minimal Example¶
require "typed_bus"
bus = TypedBus::MessageBus.new
bus.add_channel(:events, timeout: 5)
bus.subscribe(:events) do |delivery|
puts "Received: #{delivery.message}"
delivery.ack!
end
Async do
bus.publish(:events, "hello world")
end
What Just Happened¶
- Created a bus —
MessageBus.newcreates a registry for channels with shared stats. - Added a channel —
:eventsis a named topic with a 5-second delivery timeout. - Subscribed — the block receives a
Deliveryenvelope for each published message. The subscriber must callack!ornack!. - Published inside Async — channel operations create Async tasks internally, so
publishmust run within an Async reactor.
Using Channels Directly¶
You don't need a MessageBus. Channels work standalone:
require "typed_bus"
channel = TypedBus::Channel.new(:greetings, timeout: 5)
channel.subscribe do |delivery|
puts delivery.message
delivery.ack!
end
Async do
channel.publish("Hello, world!")
end
Next Steps¶
- Configuration — set defaults at global, bus, and channel levels
- Typed Channels — constrain messages to a specific class
- Delivery and Acknowledgment — understand ack/nack lifecycle