Elixir Language Support
Elixir Language Support
Section titled “Elixir Language Support”Status: ✅ Full Support Examples:
examples/elixir-basic/- Basic protobuf messagesexamples/elixir-grpc/- gRPC services
Elixir support provides Protocol Buffer and gRPC integration for building distributed systems, real-time applications, and microservices.
Available Plugins
Section titled “Available Plugins”| Plugin | Description | Generated Files |
|---|---|---|
protoc-gen-elixir | Base messages & gRPC | *.pb.ex |
Configuration
Section titled “Configuration”Basic Configuration
Section titled “Basic Configuration”languages.elixir = { enable = true; outputPath = "lib/proto";};Full Configuration
Section titled “Full Configuration”languages.elixir = {
enable = true;
outputPath = "lib/proto";
namespace = "MyApp.Proto";
options = [];
# Enable gRPC service generation
grpc = {
enable = true;
options = [];
};
# Enable validation support
validate = {
enable = true;
options = [];
};
# Compile specific proto files for Elixir
files = [
"./proto/services/v1/user_service.proto"
"./proto/messages/v1/common.proto"
];
# Additional proto files beyond the global list
additionalFiles = [
"./proto/internal/v1/admin.proto"
];
};
Features
Section titled “Features”Message Generation
Section titled “Message Generation”Generates Elixir modules for all protobuf messages with:
- Full type safety using structs
- Binary encoding/decoding
- JSON serialization support
- Default values and field presence tracking
gRPC Support
Section titled “gRPC Support”When enabled, generates:
- Service modules with server behavior
- Client stubs for service calls
- Support for all RPC types (unary, streaming, bidirectional)
- Error handling with proper gRPC status codes
Validation Support
Section titled “Validation Support”Provides hooks for integrating with validation libraries:
- Field validation rules
- Custom validation functions
- Integration with Ecto changesets
Usage Example
Section titled “Usage Example”Basic Message Usage
Section titled “Basic Message Usage”# Create a messagemessage = %MyApp.Proto.User{ id: 1, name: "Alice", email: "alice@example.com", roles: ["admin", "user"]}
# Encode to binarybinary = MyApp.Proto.User.encode(message)
# Decode from binary{:ok, decoded} = MyApp.Proto.User.decode(binary)gRPC Server Implementation
Section titled “gRPC Server Implementation”defmodule MyApp.UserService.Server do use GRPC.Server, service: MyApp.Proto.UserService.Service
def get_user(request, _stream) do user = MyApp.Users.find(request.id) %MyApp.Proto.GetUserResponse{user: user} end
def list_users(request, stream) do MyApp.Users.list() |> Stream.map(&%MyApp.Proto.User{&1}) |> Enum.each(&GRPC.Server.send_reply(stream, &1)) endendgRPC Client Usage
Section titled “gRPC Client Usage”# Connect to server{:ok, channel} = GRPC.Stub.connect("localhost:50051")
# Make RPC callrequest = %MyApp.Proto.GetUserRequest{id: 123}{:ok, response} = MyApp.Proto.UserService.Stub.get_user(channel, request)Integration with Phoenix
Section titled “Integration with Phoenix”Elixir protobuf integrates well with Phoenix applications:
defmodule MyAppWeb.ProtoController do use MyAppWeb, :controller
def show(conn, %{"id" => id}) do user = MyApp.Users.get(id) proto = %MyApp.Proto.User{user}
conn |> put_resp_content_type("application/x-protobuf") |> send_resp(200, MyApp.Proto.User.encode(proto)) endendMix.exs Dependencies
Section titled “Mix.exs Dependencies”Add these dependencies to your mix.exs:
defp deps do [ {:protobuf, "~> 0.12.0"}, {:grpc, "~> 0.7.0"}, # If using gRPC {:jason, "~> 1.4"} # For JSON support ]endConfiguration Options
Section titled “Configuration Options”languages.elixir
Section titled “languages.elixir”| Option | Type | Default | Description |
|---|---|---|---|
enable | bool | false | Enable Elixir code generation |
package | package | protoc-gen-elixir | The protoc plugin package |
outputPath | string | [string] | "lib" | Output directory for generated code |
options | [string] | [] | Options to pass to protoc-gen-elixir |
namespace | string | "" | Module namespace prefix (e.g., “MyApp.Proto”) |
files | [string] | null | null | Specific proto files for this language |
additionalFiles | [string] | [] | Additional proto files to compile |
languages.elixir.grpc
Section titled “languages.elixir.grpc”| Option | Type | Default | Description |
|---|---|---|---|
enable | bool | false | Enable gRPC code generation |
package | package | protoc-gen-elixir | The protoc plugin package |
options | [string] | [] | Options for gRPC generation |
languages.elixir.validate
Section titled “languages.elixir.validate”| Option | Type | Default | Description |
|---|---|---|---|
enable | bool | false | Enable validation support |
package | package | null | Validation package (if any) |
options | [string] | [] | Options for validation generation |
Tips and Best Practices
Section titled “Tips and Best Practices”-
Module Organization: Use the
namespaceoption to organize generated modules under your application’s namespace. -
OTP Integration: Generated gRPC servers integrate with OTP supervision trees:
children = [{GRPC.Server.Supervisor, endpoint: MyApp.Endpoint, port: 50051}] -
Error Handling: Use proper gRPC status codes:
raise GRPC.RPCError, status: :not_found, message: "User not found" -
Testing: Use the generated modules in tests:
test "encodes and decodes messages" dooriginal = %MyApp.Proto.User{id: 1, name: "Test"}binary = MyApp.Proto.User.encode(original){:ok, decoded} = MyApp.Proto.User.decode(binary)assert decoded == originalend -
Performance: For high-performance scenarios, consider using binary pattern matching directly on encoded messages.
Common Issues
Section titled “Common Issues”Module Not Found
Section titled “Module Not Found”If you get “module not found” errors after generation:
- Ensure the
outputPathis in your Elixir project’s lib path - Run
mix compileto compile the generated modules - Check that the namespace matches your project structure
gRPC Connection Issues
Section titled “gRPC Connection Issues”For gRPC connection problems:
- Verify the server is running on the correct port
- Check firewall settings
- Ensure both client and server use the same proto definitions