glixir/pubsub
This module provides a phantom-typed interface to Phoenix PubSub.
All PubSub systems are parameterized by their message_type
,
and use JSON serialization for type-safe cross-process communication.
IMPORTANT: PubSub names must be pre-existing atoms to prevent atom table overflow. Users are responsible for creating atoms safely before calling start().
Types
pub type MessageDecoder(message_type) =
fn(String) -> Result(message_type, String)
pub type MessageEncoder(message_type) =
fn(message_type) -> String
pub type PubSubBroadcastResult {
PubsubBroadcastOk
PubsubBroadcastError(reason: dynamic.Dynamic)
}
Constructors
-
PubsubBroadcastOk
-
PubsubBroadcastError(reason: dynamic.Dynamic)
Errors from PubSub operations
pub type PubSubError {
StartError(String)
SubscribeError(String)
BroadcastError(String)
UnsubscribeError(String)
EncodeError(String)
DecodeError(String)
}
Constructors
-
StartError(String)
-
SubscribeError(String)
-
BroadcastError(String)
-
UnsubscribeError(String)
-
EncodeError(String)
-
DecodeError(String)
pub type PubSubStartResult {
PubsubStartOk(pid: process.Pid)
PubsubStartError(reason: dynamic.Dynamic)
}
Constructors
-
PubsubStartOk(pid: process.Pid)
-
PubsubStartError(reason: dynamic.Dynamic)
pub type PubSubSubscribeResult {
PubsubSubscribeOk
PubsubSubscribeError(reason: dynamic.Dynamic)
}
Constructors
-
PubsubSubscribeOk
-
PubsubSubscribeError(reason: dynamic.Dynamic)
pub type PubSubUnsubscribeResult {
PubsubUnsubscribeOk
PubsubUnsubscribeError(reason: dynamic.Dynamic)
}
Constructors
-
PubsubUnsubscribeOk
-
PubsubUnsubscribeError(reason: dynamic.Dynamic)
Values
pub fn broadcast(
pubsub_name: atom.Atom,
topic: String,
message: message_type,
encode: fn(message_type) -> String,
) -> Result(Nil, PubSubError)
Broadcast a message to all subscribers using JSON serialization
pub fn int_decoder(json_string: String) -> Result(Int, String)
JSON decoder for Int messages
pub fn start(
name: atom.Atom,
) -> Result(PubSub(message_type), PubSubError)
Start a phantom-typed PubSub system with bounded message types
IMPORTANT: The atom must already exist to prevent atom table overflow. Use atom.create_from_string() or atom.from_string() safely before calling this.
pub fn string_decoder(
json_string: String,
) -> Result(String, String)
JSON decoder for String messages
pub fn string_encoder(message: String) -> String
JSON encoder for String messages
pub fn subscribe(
pubsub_name: atom.Atom,
topic: String,
gleam_module: String,
gleam_function: String,
) -> Result(Nil, PubSubError)
Subscribe to a topic with message handling The gleam_function must accept a single String parameter (JSON) and return Nil Subscribe to a topic with message handling
pub fn subscribe_with_registry_key(
pubsub_name: atom.Atom,
topic: String,
gleam_module: String,
gleam_function: String,
registry_key: String,
) -> Result(Nil, PubSubError)
Subscribe to a topic with registry key for direct actor targeting
pub fn unsubscribe(
pubsub_name: atom.Atom,
topic: String,
) -> Result(Nil, PubSubError)
Unsubscribe from a topic