Ruby Quick Start

This guide gets you started with gRPC in Ruby with a simple working example.

Prerequisites

  • Ruby version 2 or higher

gRPC

To install gRPC, run the following command:

  1. $ gem install grpc

gRPC tools

Ruby’s gRPC tools include the protocol buffer compiler protoc and the specialplugin for generating server and client code from the .proto servicedefinitions. For the first part of our quick-start example, we’ve alreadygenerated the server and client stubs fromhelloworld.proto,but you’ll need the tools for the rest of our quick start, as well as latertutorials and your own projects.

To install gRPC tools, run the following command:

  1. $ gem install grpc-tools

Download the example

You’ll need a local copy of the example code to work through this quick start.Download the example code from our GitHub repository (the following commandclones the entire repository, but you just need the examples for this quick startand other tutorials):

  1. # Clone the repository to get the example code:
  2. $ git clone -b v1.28.1 https://github.com/grpc/grpc
  3. # Navigate to the "hello, world" Ruby example:
  4. $ cd grpc/examples/ruby

Run a gRPC application

From the examples/ruby directory:

  • Run the server:
  1. $ ruby greeter_server.rb
  • From another terminal, run the client:
  1. $ ruby greeter_client.rb

Congratulations! You’ve just run a client-server application with gRPC.

Update a gRPC service

Now let’s look at how to update the application with an extra method on theserver for the client to call. Our gRPC service is defined using protocolbuffers; you can find out lots more about how to define a service in a .protofile ingRPC Basics: Ruby. For now all you needto know is that both the server and the client “stub” have a SayHello RPCmethod that takes a HelloRequest parameter from the client and returns aHelloResponse from the server, and that this method is defined like this:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. }
  6. // The request message containing the user's name.
  7. message HelloRequest {
  8. string name = 1;
  9. }
  10. // The response message containing the greetings
  11. message HelloReply {
  12. string message = 1;
  13. }

Let’s update this so that the Greeter service has two methods. Editexamples/protos/helloworld.proto and update it with a new SayHelloAgainmethod, with the same request and response types:

  1. // The greeting service definition.
  2. service Greeter {
  3. // Sends a greeting
  4. rpc SayHello (HelloRequest) returns (HelloReply) {}
  5. // Sends another greeting
  6. rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
  7. }
  8. // The request message containing the user's name.
  9. message HelloRequest {
  10. string name = 1;
  11. }
  12. // The response message containing the greetings
  13. message HelloReply {
  14. string message = 1;
  15. }

Remember to save the file!

Generate gRPC code

Next we need to update the gRPC code used by our application to use the newservice definition. From the examples/ruby/ directory:

  1. $ grpc_tools_ruby_protoc -I ../protos --ruby_out=lib --grpc_out=lib ../protos/helloworld.proto

This regenerates lib/helloworld_services_pb.rb, which contains our generatedclient and server classes.

Update the server

In the same directory, open greeter_server.rb. Implement the new method like this

  1. class GreeterServer < Helloworld::Greeter::Service
  2. def say_hello(hello_req, _unused_call)
  3. Helloworld::HelloReply.new(message: "Hello #{hello_req.name}")
  4. end
  5. def say_hello_again(hello_req, _unused_call)
  6. Helloworld::HelloReply.new(message: "Hello again, #{hello_req.name}")
  7. end
  8. end

Update the client

In the same directory, open greeter_client.rb. Call the new method like this:

  1. def main
  2. stub = Helloworld::Greeter::Stub.new('localhost:50051', :this_channel_is_insecure)
  3. user = ARGV.size > 0 ? ARGV[0] : 'world'
  4. message = stub.say_hello(Helloworld::HelloRequest.new(name: user)).message
  5. p "Greeting: #{message}"
  6. message = stub.say_hello_again(Helloworld::HelloRequest.new(name: user)).message
  7. p "Greeting: #{message}"
  8. end

Run!

Just like we did before, from the examples/ruby directory:

  • Run the server:
  1. $ ruby greeter_server.rb
  • From another terminal, run the client:
  1. $ ruby greeter_client.rb

What’s next