Hasty Briefsbeta

Bilingual

Calling Go from Elixir with a CNode in Crystal

a year ago
  • #Crystal
  • #Go
  • #Elixir
  • Mozi needed to connect an Elixir Phoenix LiveView app to an existing Go backend.
  • Options considered included NIFs (Native Implemented Functions) and Ports, but both had drawbacks.
  • The chosen solution was a 'C Node' using Erlang's erl_interface library for BEAM distribution.
  • A C wrapper was built to call Go code, with main() in C and Go initialized from there.
  • The C node connects to the Elixir app via a well-known local address, managed by S6 in a container.
  • Ergo, a Go implementation of BEAM distribution, was considered but not used due to compatibility and licensing issues.
  • The C code was later rewritten in Crystal for better maintainability, with thin wrappers for erl_interface.
  • The system is deployed as a single Docker container in Kubernetes, with independent builds for Elixir and Go/Crystal components.
  • A custom Go compiler build was temporarily needed for Alpine Linux support, but this will soon be unnecessary.
  • Interest was expressed in open-sourcing the Crystal wrapper for erl_interface.