Events
An event is a one-way broadcast. A publisher fires a message and moves on; any number of subscribers react independently, and nothing is returned. Events extend PostboyGenericMessage and use the fire, sub, and once verbs. When the sender needs a result, use a callback message instead — see Callbacks.
When to use events
Use events for domain notifications, UI broadcasts, and state-change signals with zero or many listeners. Do not use them for request/response (use callbacks) or when a value is needed synchronously (use executors).
Define an event
Register the event type
The idiomatic home for registration is a registrator:
Or connect the subject directly:
Publish with fire
fire returns void. A registered event with no subscribers is discarded silently; firing an unregistered ID throws.
Subscribe with sub and once
Subject flavors
The registration flavor decides what new subscribers see.
Plain subject (recordSubject, or new Subject() with ConnectMessage) — subscribers only receive messages fired after they subscribed.
Replay (recordReplay) — new subscribers immediately receive the latest fired message:
Behavior (recordBehavior) — the event models state; every subscriber receives the current message, and the initial value is a message instance:
Subscription hygiene
subreturns anObservable<T>, not aSubject. Never call.next()on it — publish withfire.Keep the
Subscriptionreturned by.subscribe(...)and unsubscribe on teardown;oncecompletes by itself after the first message.Subscribe after registration.
subon an unregistered type returns a cold observable that never emits.
Pitfalls
Calling
.next()on the result ofsub— it is anObservable; publishing goes throughfire.Firing before registration — the bus throws for unregistered IDs.
Re-registering the same type ad hoc — the second registration silently overwrites the first; group registrations in one registrator.
Expecting replay semantics from a plain subject — late subscribers miss earlier messages; use
recordReplay.
Next steps
Event Patterns — broadcasting, multi-component subscriptions, state broadcasts