A lightweight, high-performance, and purely idiomatic gRPC framework for Elixir.
- Zero External Compilers: No
protocbinary or C/NIF dependencies required. Parses.protofiles directly in pure Elixir at compile-time or runtime. - Cowboy Native: Serves native gRPC and gRPC-Web over HTTP/2 on Cowboy, with native HTTP/2 trailers.
- Pure Elixir HTTP/2 Client: Native HTTP/2 client transport powered by Mint.
- Lightning-Fast In-Memory Testing: Includes
SimpleGrpc.Caseand an in-memory transport to test services with zero TCP overhead. - Built-in Reflection: First-class support for
grpc.reflection.v1andgrpc.reflection.v1alphaout of the box. - Full Streaming Support: Unary, Server Streaming, Client Streaming, and Bidirectional Streaming.
- Telemetry Ready: Native
:telemetryinstrumentation on both client and server.
Add simple_grpc to your list of dependencies in mix.exs:
def deps do
[
{:simple_grpc, "~> 0.1.0"}
]
endHow simple_grpc thinks about proto files, services versus servers, messages and routes
is described in docs/concepts.md. It also lists which parts of that
model are implemented today; the examples below show the current API.
Define a gRPC server module pointing to your .proto file. server_file: is the shorthand
for a single self-contained file. A service that imports other files lists all of them with
files: [...] instead; entries may be globs like "priv/protos/greeter/*.proto" (see
docs/concepts.md):
defmodule MyEchoServer do
use SimpleGrpc.Server,
server_file: "priv/protos/echo_service.proto"
# In-module handler for Echo RPC
def echo(%{message: msg}, _stream) do
{:ok, %{reply: "Echo: " <> msg}}
end
endAdd SimpleGrpc.Cowboy to your supervision tree with the services to serve:
children = [
{SimpleGrpc.Cowboy,
services: [MyApp.GreeterServer, MyApp.AdminServer],
port: 50051}
]One listener serves every listed service. Each service answers only requests under
its own /<package>.<Service>/ prefix, so services that share RPC names or messages
never mix. Options you can pass alongside :services:
| Option | Meaning |
|---|---|
:services |
Server modules to serve. |
:reflection |
true, or [only: [...]] / [except: [...]] to scope gRPC reflection. |
:health |
true to serve the canonical grpc.health.v1.Health service. |
:interceptors |
Server-side interceptors applied to every call. |
:cors |
true, or a keyword list, for gRPC-Web CORS. |
:tls |
TLS options such as :certfile and :keyfile. |
:port |
Listen port (default 50051). |
Without :services the listener serves whatever is registered in the global
SimpleGrpc.Registry:
SimpleGrpc.register_service(MyEchoServer)
children = [{SimpleGrpc.Cowboy, port: 50051}]Reflection can be scoped to a subset of the served services:
{SimpleGrpc.Cowboy,
services: [MyEchoServer, MyAdminServer],
reflection: [only: [MyEchoServer]],
port: 50051}Serve over TLS by passing :tls options, and tune gRPC-Web CORS with :cors:
{SimpleGrpc.Cowboy,
services: [MyEchoServer],
port: 50051,
tls: [certfile: "priv/certs/server.crt", keyfile: "priv/certs/server.key"],
cors: [allowed_origins: ["https://app.example.com"]]}SimpleGrpc handles all 4 gRPC streaming patterns lazily over HTTP/2 without buffering entire streams into memory:
| Pattern | Handler Signature | Data Flow & Memory |
|---|---|---|
| Unary | def call(req, _stream) -> {:ok, %{...}} |
Single bounded request/response. |
| Server Streaming | def call(req, _stream) -> Enumerable.t() |
Emits a lazy Stream. Each item is immediately encoded and flushed via HTTP/2 chunking ( |
| Client Streaming | def call(req_stream, _stream) -> {:ok, %{...}} |
Receives a lazy Stream. Frames are read and decoded on demand as the handler iterates (e.g. Enum.reduce/3). |
| Bidirectional Streaming | def call(req_stream, _stream) -> Enumerable.t() |
Consumes request frames and emits response chunks concurrently over the active HTTP/2 stream. |
defmodule MyStreamingServer do
use SimpleGrpc.Server,
server_file: "priv/protos/streaming_service.proto"
# 1. Server Streaming: Single request -> lazy Stream of responses
def server_streaming_call(req, _stream) do
1..10_000
|> Stream.map(fn seq ->
%{result: "Data chunk #{seq} for #{req.message}", sequence: seq, is_final: seq == 10_000}
end)
end
# 2. Client Streaming: Lazy request Stream -> single response
def client_streaming_call(req_stream, _stream) do
{count, total_seq} =
Enum.reduce(req_stream, {0, 0}, fn req, {c, s} ->
{c + 1, s + req.sequence}
end)
{:ok, %{result: "Processed #{count} items", sequence: total_seq, is_final: true}}
end
# 3. Bidirectional Streaming: Lazy request Stream -> lazy response Stream
def bidirectional_streaming_call(req_stream, _stream) do
Stream.map(req_stream, fn req ->
%{result: "Echo: " <> req.message, sequence: req.sequence * 2, is_final: false}
end)
end
end-
$O(1)$ Memory Footprint: Messages are read and written frame-by-frame. You can stream gigabytes of telemetry or millions of rows with minimal RAM usage. - Immediate Time-to-First-Byte (TTFB): Response chunks begin arriving at the client within milliseconds of being generated.
-
Natural Backpressure: Because request reading is demand-driven via
Stream.resource, slow consumer processing pauses socket consumption, automatically throttling the producer via standard HTTP/2 flow control windows.
SimpleGrpc provides SimpleGrpc.Case (an ExUnit.CaseTemplate) for testing your gRPC services directly in-memory, without starting a listener or binding a TCP port. Pass the services to serve with services: (or server: for a single service):
defmodule MyEchoServerTest do
use SimpleGrpc.Case, services: [MyEchoServer]
test "invokes Echo RPC in-memory", %{conn: conn} do
# 1. Encode protobuf request payload
schema = MyEchoServer.__schema__()
req_bytes = SimpleGrpc.Protobuf.encode!("EchoRequest", %{message: "Ping"}, schema)
# 2. Call the service in-memory via grpc_call helper
{:ok, resp_bytes, _headers, _trailers} =
grpc_call(conn, "/echo.EchoService/Echo", req_bytes)
# 3. Decode and assert response
{:ok, resp} = SimpleGrpc.Protobuf.decode("EchoResponse", resp_bytes, schema)
assert resp.reply == "Echo: Ping"
end
endUse grpc_stream/4 to test server, client, or bidirectional streaming:
test "tests server streaming in-memory", %{conn: conn} do
schema = MyStreamingServer.__schema__()
req_bytes = SimpleGrpc.Protobuf.encode!("StreamRequest", %{message: "Start"}, schema)
{:ok, resp_stream, _headers, _trailers} =
grpc_stream(conn, "/streaming.StreamingService/ServerStreamingCall", req_bytes)
items =
resp_stream
|> Enum.map(fn chunk ->
{:ok, decoded} = SimpleGrpc.Protobuf.decode("StreamResponse", chunk, schema)
decoded
end)
assert length(items) == 3
endPass the services to serve in use SimpleGrpc.Case. With no services the connection falls
back to the global SimpleGrpc.Registry. Override the services for a single test with an
ExUnit tag:
defmodule CustomTest do
use SimpleGrpc.Case, services: [MyEchoServer]
# Serve only this service for this test
@tag services: [IsolatedServer]
test "calls isolated service", %{conn: conn} do
{:ok, resp_bytes, _h, _t} = grpc_call(conn, "/custom.Service/Action", req_bytes)
assert resp_bytes != nil
end
endSimpleGrpc.Client connects over network HTTP/2 (via Mint) or directly in-memory (via the :memory adapter):
# 1. Connect over HTTP/2 TCP
{:ok, conn} = SimpleGrpc.Client.connect("127.0.0.1:50051", adapter: :mint)
# 2. Send Unary RPC
{:ok, resp_bytes, headers, trailers} =
SimpleGrpc.Client.send_unary(conn, "/echo.EchoService/Echo", req_bytes)
# 3. Send Streaming RPC
{:ok, resp_stream, headers, trailers} =
SimpleGrpc.Client.send_stream(conn, "/streaming.StreamingService/ServerStreamingCall", req_bytes)
# 4. Close connection
:ok = SimpleGrpc.Client.close(conn)SimpleGrpc includes built-in reflection compatible with grpcurl, Postman, and BloomRPC. Enable it on the listener:
{SimpleGrpc.Cowboy, services: [MyEchoServer], reflection: true, port: 50051}Or, with the global SimpleGrpc.Registry, enable reflection on the registered services:
SimpleGrpc.enable_reflection()Inspect services and invoke methods without providing .proto files:
# Discover services
grpcurl -plaintext localhost:50051 list
# Describe a service
grpcurl -plaintext localhost:50051 describe streaming.StreamingService
# Invoke an RPC directly via reflection
grpcurl -plaintext -d '{"message": "Hello!"}' localhost:50051 streaming.StreamingService/UnaryCallSimpleGrpc emits :telemetry events on both the server and client:
- Server Lifecycle:
[:simple_grpc, :call, :start]— measurements:%{system_time: system_time, monotonic_time: monotonic_time}, metadata:%{path: path, headers: req_headers, service: service, rpc: rpc}[:simple_grpc, :call, :stop]— measurements:%{duration: duration, monotonic_time: monotonic_time}, metadata:%{path: path, request: req, response: resp, grpc_status: status, grpc_message: message}[:simple_grpc, :call, :exception]— measurements:%{duration: duration, monotonic_time: monotonic_time}, metadata:%{path: path, kind: kind, reason: reason, stacktrace: stacktrace}
- Client Lifecycle:
[:simple_grpc, :client, :start]— measurements:%{system_time: system_time, monotonic_time: monotonic_time}[:simple_grpc, :client, :stop]— measurements:%{duration: duration, monotonic_time: monotonic_time}, metadata adds%{response: resp, grpc_status: status, grpc_message: message}[:simple_grpc, :client, :exception]— measurements:%{duration: duration, monotonic_time: monotonic_time}, metadata adds%{kind: kind, reason: reason, stacktrace: stacktrace}