Skip to content
An Elixir implementation of gRPC
Elixir Erlang Other
Branch: master
Clone or download
Latest commit e452825 Jul 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmark add benchmark to README Jul 7, 2019
config format code Mar 21, 2018
examples doc(example): fix benchmark notes Jul 7, 2019
interop
lib handle exceptions on the outermost layer (#96) Jul 15, 2019
priv/tls use protobuf to replace exprotobuf Aug 5, 2017
src fix flow control size Feb 27, 2019
test bump to 0.4.0 Jul 15, 2019
.formatter.exs fix and doc for interceptor Dec 19, 2018
.gitignore benchmark: add client and refactor a little Jul 24, 2018
.travis.yml reduce rounds in travis cron Jul 12, 2019
Dockerfile Dockerized the library. Apr 9, 2019
LICENSE add LICENSE Nov 14, 2016
Makefile add Makefile Mar 22, 2019
README.md add benchmark to README Jul 7, 2019
mix.exs bump to 0.4.0 Jul 15, 2019
mix.lock update gun Jun 3, 2019

README.md

gRPC Elixir

Hex.pm Build Status Inline docs

An Elixir implementation of gRPC.

WARNING: Be careful to use it in production! Test and benchmark in advance.

NOTICE: Erlang/OTP needs >= 20.3.2

Installation

The package can be installed as:

  1. Add grpc to your list of dependencies in mix.exs:

    def deps do
      [{:grpc, github: "elixir-grpc/grpc"}]
    end
  2. (Before Elixir 1.4)Ensure grpc is started before your application:

    def application do
      [applications: [:grpc]]
    end

Usage

  1. Generate Elixir code from proto file as protobuf-elixir shows(especially the gRPC Support section).
  2. Implement the server side code like below and remember to return the expected message types.
defmodule Helloworld.Greeter.Server do
  use GRPC.Server, service: Helloworld.Greeter.Service

  @spec say_hello(Helloworld.HelloRequest.t, GRPC.Server.Stream.t) :: Helloworld.HelloReply.t
  def say_hello(request, _stream) do
    Helloworld.HelloReply.new(message: "Hello #{request.name}")
  end
end
  1. Start the server

You can start the gRPC server as a supervised process. First, add GRPC.Server.Supervisor to your supervision tree.

# Define your endpoint
defmodule Helloworld.Endpoint do
  use GRPC.Endpoint

  intercept GRPC.Logger.Server
  run Helloworld.Greeter.Server
end

# In the start function of your Application
defmodule HelloworldApp do
  use Application
  def start(_type, _args) do
    children = [
      # ...
      supervisor(GRPC.Server.Supervisor, [{Helloworld.Endpoint, 50051}])
    ]

    opts = [strategy: :one_for_one, name: YourApp]
    Supervisor.start_link(children, opts)
  end
end

Then start it when starting your application:

# config.exs
config :grpc, start_server: true

# test.exs
config :grpc, start_server: false

$ iex -S mix

or run grpc.server using a mix task

$ mix grpc.server
  1. Call rpc:
iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051")
iex> request = Helloworld.HelloRequest.new(name: "grpc-elixir")
iex> {:ok, reply} = channel |> Helloworld.Greeter.Stub.say_hello(request)

# With interceptors
iex> {:ok, channel} = GRPC.Stub.connect("localhost:50051", interceptors: [GRPC.Logger.Client])
...

Check examples and interop(Interoperability Test) for some examples.

TODO

  • Unary RPC
  • Server streaming RPC
  • Client streaming RPC
  • Bidirectional streaming RPC
  • Helloworld and RouteGuide examples
  • Doc and more tests
  • Authentication with TLS
  • Improve code generation from protos (protobuf-elixir #8)
  • Timeout for unary calls
  • Errors handling
  • Benchmarking
  • Logging
  • Interceptors(See GRPC.Endpoint)
  • Connection Backoff
  • Data compression
  • Support other encoding(other than protobuf)

Benchmark

  1. Simple benchmark by using ghz

  2. Benchmark followed by official spec

Sponsors

This project is being sponsored by Tubi. Thank you!

Contributing

You contributions are welcome!

Please open issues if you have questions, problems and ideas. You can create pull requests directly if you want to fix little bugs, add small features and so on. But you'd better use issues first if you want to add a big feature or change a lot of code.

You can’t perform that action at this time.