Integrating gRPC with Ruby on Rails: A Fusion of Technologies for Enhanced Web Development

Integrating gRPC with Ruby on Rails

Alessio Bussolari
3 min readNov 21, 2023

--

In the world of modern microservices architectures, efficient inter-service communication is crucial. gRPC (Google Remote Procedure Call) is a high-performance, language-agnostic framework that provides an efficient way for services to communicate with each other. In this article, we’ll explore how to integrate gRPC into a Ruby on Rails application, enabling seamless and performant communication between services.

What is gRPC?

gRPC is a remote procedure call (RPC) framework developed by Google. It uses HTTP/2 for transport and Protocol Buffers (Protobuf) as its interface definition language. This combination offers several advantages, such as high performance, language-agnostic support, and strong typing. gRPC is well-suited for building efficient microservices because it allows services to communicate with minimal overhead.

Setting up a Ruby on Rails Project

Before we dive into integrating gRPC, let’s set up a Ruby on Rails project if you don’t have one already. You can create a new Rails project using the following command:

rails new my_grpc_app

Once your Rails project is ready, let’s proceed to integrate gRPC.

Adding gRPC to Your Rails Application

To add gRPC support to your Rails application, you’ll need to use the grpc gem. Add it to your Gemfile and run bundle install:

# Gemfile
gem 'grpc'

After installing the gem, you’ll need to create a gRPC service definition file (a .proto file) that defines the service methods and message types. This file will serve as the contract between your services. Here’s a simple example of a .proto file:

// hello.proto
syntax = "proto3";

service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}

message HelloRequest {
string name = 1;
}

message HelloReply {
string message = 1;
}

In this example, we define a Greeter service with a SayHello method that takes a HelloRequest and returns a HelloReply.

Generating Ruby Code from the .proto File

--

--

Alessio Bussolari

Ruby on Rails programmer since 2009. Current CTO at COSMIC SRL, where I lead the team in creating innovative solutions.