Examples
This folder contains runnable integration examples demonstrating type-safe distributed messaging with the distribute library.
These examples use the new GlobalSubject with integrated codecs, showing compile-time type safety for cross-node communication.
Prerequisites
gleamin yourPATH- Erlang/OTP installed (so
erlanderlcare available)
Available Examples
1. two_nodes/ — Basic Type-Safe Messaging
Demonstrates:
- Creating
GlobalSubjectwith codecs - Type-safe global registration
- Type-safe message sending with
send_global_typed
Run:
./examples/two_nodes/integration_test.sh
2. two_nodes_app/ — SWIM Membership + Type-Safe Messaging
Demonstrates:
GlobalSubjectwith membership service- Leader election
- Type-safe distributed messaging
Run:
./examples/two_nodes_app/scripts/run_integration.sh
3. typed_messaging/ — Advanced Type-Safe Examples
Demonstrates:
GlobalSubjectwith custom codecs- Type-safe process groups
- Type-safe broadcast
- Receiving typed messages with
global.receive(global, timeout_ms)
Run:
# Terminal 1
gleam run -m examples/typed_messaging/typed_node_a
# Terminal 2
gleam run -m examples/typed_messaging/typed_node_b
Example of receiving a message:
case global.receive(my_global, 5_000) {
Ok(msg) -> // use msg
Error(_) -> // timeout or decode error
}
What’s New in v2.0
All examples now use:
- ✅ GlobalSubject with integrated encoder/decoder
- ✅ Type-safe APIs (
send_global_typed,broadcast_typed) - ✅ No unsafe Erlang terms - everything goes through codecs
- ✅ Compile-time type checking for distributed messages
Integration Tests
The two_nodes/ directory also contains additional integration test scripts:
swim_integration.sh- SWIM membership protocol test (moved from test/)partition_rejoin.sh- Network partition handlingraft_failover_test.sh- Leader election failover