September 4, 2019

building a gRPC application (part 6)

In Part Six of this tutorial series on gRPC, we will modify our service the wrong way to make it a bit more interesting.

The simple service we’ve created isn’t all that interesting. Let’s say we now would like to send a message with the acknowledgement. We’ll create a new protocol message:

message UserAcknowledgement {
  UserInformation user = 1;
  string message = 2;

that will send back the original information along with a message string. Then we’ll ammend the service definition:

service UserActivity {
  rpc Acknowledge(UserInformation) returns (UserAcknowledgement) {}

Our implementation, of course, must change:

require './lib/user_services_pb'

module Demo
  class Friendly < UserActivity::Service
    def acknowledge(user_information, _extra)
        user: user_information.username,
        message: "Hello #{user_information.username}!"

We need to re-generate the bindings, too:

grpc_tools_ruby_protoc -I../../protocols --ruby_out=lib --grpc_out=lib ../../protocols/*.proto

Fix the require bug by opening up user_services_pb.rb and changing require 'user_pb' to require_relative 'user_pb'.

Then re-run the script and retest in the repl:

pry(main)> friendly.acknowledge steve
=> <UserAcknowledgement: user: <UserInformation: username: "steve">, message: "Hello steve!">

Good; we’ve a slightly more interesting service. Note, however, we’ve done so in a way that violates orthodoxy, which says that protocol buffer interfaces should be added and never modified or removed. By extension, that can be understood to inlcude the gRPC service definitions. As this is a small demonstration, written as an incremental commit, I’ve relaxed from that orthodoxy. In general, however, one should not make such modifications in main branch code, as it potentially breaks the contract with already-deployed clients or services.

Find the code for this commit on Gitlab:

Content by © Jared Davis 2019-2020

Powered by Hugo & Kiss.