Subscriptions
Subscribe
Section titled “Subscribe”channel.subscribe(pattern, callback) registers a callback for messages matching the pattern:
The callback receives the full Envelope, which includes id, type, channel, topic, payload, and timestamp.
Unsubscribe
Section titled “Unsubscribe”subscribe returns an unsubscribe function. Call it to remove the subscription:
Calling the unsubscribe function multiple times is safe — subsequent calls are no-ops.
Multiple subscribers
Section titled “Multiple subscribers”Multiple subscribers can match the same topic. All of them receive the envelope:
Error isolation
Section titled “Error isolation”If a subscriber throws, the remaining subscribers still execute. Errors are collected and re-thrown as a single AggregateError after all subscribers have run:
Publish
Section titled “Publish”channel.publish(topic, payload) creates an envelope and dispatches it to all matching subscribers:
Topics are exact strings — no wildcards in publish. Wildcards are only for subscription patterns.
Request / Handle
Section titled “Request / Handle”postal includes built-in RPC via request() and handle().
Registering a handler
Section titled “Registering a handler”Only one handler can be registered per topic per channel. Attempting to register a second handler for the same topic throws immediately.
Handlers can be synchronous or async:
Sending a request
Section titled “Sending a request”request() returns a Promise that resolves with the handler’s return value.
Timeouts
Section titled “Timeouts”Requests time out after 5 seconds by default. Customize with the timeout option:
If no response arrives in time, the Promise rejects with PostalTimeoutError:
Error propagation
Section titled “Error propagation”If a handler throws, the error is caught and relayed to the requester as PostalRpcError:
Handlers can throw errors with a code property for machine-readable classification:
Regular subscribers see requests
Section titled “Regular subscribers see requests”Request envelopes flow through the normal subscriber list too — any subscriber matching the topic will see it. This is useful for logging or auditing RPC traffic: