An Elixir implementation of gRPC.
Note The Gun library doesn't have a full 2.0 release yet, so we depend on
:grcp_gun 2.0.1for now. This is the same as:gun 2.0.0-rc.2, but Hex doesn't let us depend on RC versions for releases.
The package can be installed as:
defdepsdo[{:grpc,"~> 0.5.0"},# We don't force protobuf as a dependency for more# flexibility on which protobuf library is used,# but you probably want to use it as well{:protobuf,"~> 0.10"}]endGenerate Elixir code from proto file as protobuf-elixir shows(especially the
gRPC Supportsection).Implement the server side code like below and remember to return the expected message types.
defmoduleHelloworld.Greeter.ServerdouseGRPC.Server,service: Helloworld.Greeter.Service@specsay_hello(Helloworld.HelloRequest.t,GRPC.Server.Stream.t)::Helloworld.HelloReply.tdefsay_hello(request,_stream)doHelloworld.HelloReply.new(message: "Hello #{request.name}")endend- Start the server
You can start the gRPC server as a supervised process. First, add GRPC.Server.Supervisor to your supervision tree.
# Define your endpointdefmoduleHelloworld.EndpointdouseGRPC.EndpointinterceptGRPC.Logger.ServerrunHelloworld.Greeter.Serverend# In the start function of your ApplicationdefmoduleHelloworldAppdouseApplicationdefstart(_type,_args)dochildren=[# ...{GRPC.Server.Supervisor,endpoint: Helloworld.Endpoint,port: 50051}]opts=[strategy: :one_for_one,name: YourApp]Supervisor.start_link(children,opts)endend- 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 interceptorsiex>{:ok,channel}=GRPC.Stub.connect("localhost:50051",interceptors: [GRPC.Logger.Client])...Check examples and interop(Interoperability Test) for some examples.
- Various kinds of RPC:
- TLS Authentication
- Error handling
- Interceptors(See
GRPC.Endpoint) - Connection Backoff
- Data compression
Simple benchmark by using ghz
Benchmark followed by official spec
Your 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.