refrakt/pubsub

Topic-based publish/subscribe for real-time messaging.

Experimental. This module is lightly used and may change or move to its own package; see https://github.com/raskell-io/refrakt/issues/28.

A single OTP actor tracks subscribers per topic and dispatches messages.

Usage

// Start the PubSub system
let assert Ok(ps) = pubsub.start()

// Subscribe to a topic
let subject = process.new_subject()
pubsub.subscribe(ps, "chat:lobby", subject)

// Publish a message
pubsub.broadcast(ps, "chat:lobby", "Hello everyone!")

// All subscribers on "chat:lobby" receive the message
let assert Ok(msg) = process.receive(subject, 1000)
// msg == "Hello everyone!"

Types

A running PubSub instance.

pub type PubSub(msg) =
  process.Subject(PubSubMessage(msg))

Messages the PubSub actor handles.

pub opaque type PubSubMessage(msg)

Values

pub fn broadcast(
  ps: process.Subject(PubSubMessage(msg)),
  topic: String,
  message: msg,
) -> Nil

Broadcast a message to all subscribers of a topic.

pub fn start() -> Result(
  process.Subject(PubSubMessage(msg)),
  actor.StartError,
)

Start a new PubSub actor.

pub fn subscribe(
  ps: process.Subject(PubSubMessage(msg)),
  topic: String,
  subscriber: process.Subject(msg),
) -> Nil

Subscribe a subject to a topic.

pub fn unsubscribe(
  ps: process.Subject(PubSubMessage(msg)),
  topic: String,
  subscriber: process.Subject(msg),
) -> Nil

Unsubscribe a subject from a topic.

✨ Search Document