distribute

logo

Typed distributed messaging for Gleam on the BEAM.

Package VersionHex Docs

distribute is a thin typed safety layer over Erlang's distribution primitives. It puts binary codecs in front of :global and Subject so the compiler catches protocol mismatches before messages leave the process. Payload size and dead-target detection are enforced at every I/O boundary, so a misbehaving peer cannot OOM your node or hang it.

Install

gleam add distribute

Requires Gleam 1.16 or newer, Erlang target only.

30-second taste

import distribute
import distribute/codec
import distribute/receiver
let greeter = distribute.named("greeter", codec.string())
let assert Ok(_gs) =
distribute.start_registered(greeter, Nil, fn(msg, _state) {
io.println("got: " <> msg)
receiver.Continue(Nil)
})
let assert Ok(target) = distribute.lookup(greeter)
let assert Ok(Nil) = distribute.send(target, "hello")

For the full walkthrough see docs/quickstart.md.

Documentation

All long-form docs live in docs/:

What you get

Custom Type Codecs

Seamlessly encode and decode your Algebraic Data Types (enums) with a fluent builder.

pub type MyMessage {
Text(String)
Ping
}
import distribute/codec
import distribute/codec/variant
let my_codec =
variant.new()
|> variant.add(0, "Text", codec.string(), Text, fn(m) {
case m { Text(s) -> Ok(s); _ -> Error(Nil) }
})
|> variant.unit(1, "Ping", Ping, fn(m) { m == Ping })
|> variant.build()

Cluster Monitoring

Subscribe to cluster events (NodeUp, NodeDown) to react to node topology changes.

import distribute
import distribute/cluster/monitor
let subj = process.new_subject()
let assert Ok(m) = distribute.subscribe(subj)
// In your actor/process
case process.receive(subj, 5000) {
Ok(monitor.NodeUp(node)) -> io.println("Node joined: " <> node)
Ok(monitor.NodeDown(node)) -> io.println("Node left: " <> node)
_ -> Nil
}
// Later
distribute.unsubscribe(m)

Caveats

Development

gleam test # full suite (includes mandatory real-cluster Z2/Z3)
epmd -daemon # start Erlang distribution daemon if not already running
gleam dev # multi-node playground
gleam docs build # local API docs

License

MIT. See LICENSE.