Hive.Pubsub
hive · API reference
Pubsub capability and in-process implementation.
Pubsub capability interface.
hive never depends on a pubsub implementation; the application wires one in (e.g. Hcs.Pubsub) at composition time. Delivery into a server is bridged with Server.Make.subscribe, which turns deliveries into Info.Pubsub messages.
type unsubscribe = unit -> unitRelease a subscription. Returned by subscribe.
type t = {
publish : topic:string -> payload:string -> unit; (* Publish an opaque payload to a topic. *)
subscribe : topic:string -> (payload:string -> unit) -> unsubscribe; (* Subscribe a callback to a topic. *)
}Pubsub capability record.
Servers accept this record rather than depending on a concrete pubsub library.
module type TRANSPORT = sig ... endA concrete pubsub transport.
module Make (T : TRANSPORT) : sig ... endBuild a t capability from a transport module.
module Local : sig ... endIn-process transport used by local.
val local : unit -> tAn in-process implementation (kcas-backed), for tests and single-process applications. publish invokes subscriber callbacks synchronously in the publisher's fiber; callbacks that enqueue into a full mailbox will block the publisher (backpressure).
ocaml]
let bus = Hive.Pubsub.local () in
let unsubscribe = bus.subscribe ~topic:"events" (fun ~payload -> traceln "%s" payload) in
bus.publish ~topic:"events" ~payload:"ready";
unsubscribe ()