Glyn: Type-safe PubSub and Registry for Gleam actors with distributed clustering

https://news.ycombinator.com/rss Hits: 7
Summary

Glyn ✨ Type-safe PubSub and Registry for Gleam actors with distributed clustering support. Built on the Erlang syn library. Glyn provides two complementary systems for actor communication: PubSub : Broadcast events to multiple subscribers : Broadcast events to multiple subscribers Registry: Direct command routing to named processes Both systems integrate seamlessly with Gleam's actor model using selector composition patterns. Installation gleam add glyn Creating Message Types and Decoders First, define your message types and corresponding decoder functions. Explicit decoders are required to ensure messages sent between nodes in a cluster are handled with type safety. Note the Glyn does not JSON encode messages, they are sent directly as erlang terms and should be decoded from tuples. // my_app/orders.gleam import gleam/dynamic . { type Dynamic } import gleam/dynamic/decode import gleam/erlang/process . { type Subject } // Define your message types pub type Event { OrderCreated ( id : String , amount : Int ) OrderShipped ( id : String , tracking : String ) SystemAlert ( message : String ) } pub type Command { ProcessOrder ( id : String , reply_with : Subject ( Bool ) ) GetStatus ( reply_with : Subject ( String ) ) Shutdown } // Helper function to match specific atoms fn expect_atom ( expected : String ) -> decode . Decoder ( atom . Atom ) { use value <- decode . then ( atom . decoder ( ) ) case atom . to_string ( value ) == expected { True -> decode . success ( value ) False -> decode . failure ( value , "Expected atom: " <> expected ) } } // Unsafe cast for Subject decoding - use with caution @ external ( erlang , "gleam_stdlib" , "identity" ) fn unsafe_cast_subject ( value : Dynamic ) -> Subject ( a ) // Create decoder functions pub fn event_decoder ( ) -> decode . Decoder ( Event ) { decode . one_of ( { use _ <- decode . field ( 0 , expect_atom ( "order_created" ) ) use id <- decode . field ( 1 , decode . string ) use amount <- decode . field ( 2 , decode . int ) ...

First seen: 2025-08-22 23:30

Last seen: 2025-08-23 05:33