Top Related Projects
Go support for Google's protocol buffers
A simple RPC framework with protobuf service definitions
gRPC to JSON proxy generator following the gRPC HTTP spec
The official Go library for the Cloudflare API
Apache Thrift
Quick Overview
gRPC is an open-source remote procedure call (RPC) framework developed by Google. It enables efficient and scalable communication between distributed applications, allowing clients to directly call methods on a server application located in a different address space. gRPC supports various programming languages and can be used in a wide range of applications, from mobile devices to cloud services.
Pros
- High Performance: gRPC uses HTTP/2 and Protocol Buffers (Protobuf) to achieve low latency and high throughput communication.
- Language Agnostic: gRPC supports a wide range of programming languages, including C++, Java, Python, Go, C#, Node.js, and more, making it suitable for heterogeneous environments.
- Efficient Data Serialization: Protobuf, the default serialization format for gRPC, is a compact and efficient binary format, reducing the size of data transmitted over the network.
- Bidirectional Streaming: gRPC supports both unary (request-response) and streaming (client-to-server, server-to-client, and bidirectional) communication patterns, allowing for more flexible and efficient data exchange.
Cons
- Complexity: Setting up and configuring gRPC can be more complex compared to simpler RPC frameworks, especially for beginners.
- Limited Browser Support: While gRPC can be used in the browser, it requires additional tooling and setup, which may not be as straightforward as traditional web technologies.
- Learning Curve: Developers need to learn Protobuf and the gRPC-specific concepts, which can add to the initial learning curve.
- Ecosystem Maturity: Compared to more established RPC frameworks, the gRPC ecosystem may not have as many third-party libraries and tools available.
Code Examples
Here are a few code examples demonstrating the usage of gRPC in different programming languages:
Python
# Define the Protobuf service definition
from google.protobuf.internal import containers
import grpc_pb2
import grpc_pb2_grpc
class GreeterServicer(grpc_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
return grpc_pb2.HelloReply(message=f'Hello, {request.name}!')
# Create the gRPC server and register the service
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
grpc_pb2_grpc.add_GreeterServicer_to_server(GreeterServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
This example demonstrates the implementation of a simple gRPC service in Python, where a GreeterServicer
class is defined to handle the SayHello
RPC method.
Go
// Define the Protobuf service definition
type GreeterServer struct {
grpc_pb2.UnimplementedGreeterServer
}
func (s *GreeterServer) SayHello(ctx context.Context, in *grpc_pb2.HelloRequest) (*grpc_pb2.HelloReply, error) {
return &grpc_pb2.HelloReply{Message: "Hello, " + in.Name}, nil
}
// Create the gRPC server and register the service
s := grpc.NewServer()
grpc_pb2_grpc.RegisterGreeterServer(s, &GreeterServer{})
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
This Go example demonstrates a similar implementation of the GreeterServer
that handles the SayHello
RPC method.
JavaScript (Node.js)
// Define the Protobuf service definition
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const packageDefinition = protoLoader.loadSync('path/to/
Competitor Comparisons
Go support for Google's protocol buffers
Pros of protobuf
- Lightweight and focused specifically on Protocol Buffers for Go
- Simpler to use for projects that only need Protocol Buffers without gRPC
- Faster compilation times due to smaller codebase
Cons of protobuf
- Limited to Protocol Buffers functionality, lacking gRPC support
- Less comprehensive documentation compared to grpc
- Smaller community and ecosystem
Code Comparison
protobuf:
import "google.golang.org/protobuf/proto"
message := &MyMessage{...}
data, err := proto.Marshal(message)
grpc:
import "google.golang.org/grpc"
conn, err := grpc.Dial(address, grpc.WithInsecure())
client := NewMyServiceClient(conn)
response, err := client.MyMethod(context.Background(), request)
Summary
protobuf is a focused library for Protocol Buffers in Go, offering simplicity and faster compilation. However, it lacks gRPC support and has a smaller ecosystem. grpc provides a comprehensive solution for both Protocol Buffers and gRPC, with extensive documentation and community support, but may be overkill for projects only needing Protocol Buffers. Choose based on your project's specific requirements and complexity.
A simple RPC framework with protobuf service definitions
Pros of Twirp
- Simpler and more lightweight than gRPC
- Easier to set up and use, with less boilerplate code
- Supports both JSON and Protobuf encoding, offering more flexibility
Cons of Twirp
- Less feature-rich compared to gRPC (e.g., no streaming support)
- Smaller ecosystem and community support
- Limited language support (primarily Go, with some community-driven implementations)
Code Comparison
gRPC example:
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Twirp example:
service Haberdasher {
rpc MakeHat(Size) returns (Hat);
}
message Size {
int32 inches = 1;
}
message Hat {
int32 inches = 1;
string color = 2;
string name = 3;
}
Both gRPC and Twirp use Protocol Buffers for service definitions, but Twirp's implementation is generally simpler and requires less configuration. gRPC offers more advanced features like streaming and bidirectional communication, while Twirp focuses on simplicity and ease of use for basic RPC needs.
gRPC to JSON proxy generator following the gRPC HTTP spec
Pros of grpc-gateway
- Enables RESTful JSON API alongside gRPC services
- Simplifies integration with existing REST ecosystems
- Automatic OpenAPI (Swagger) documentation generation
Cons of grpc-gateway
- Additional complexity in project setup and maintenance
- Potential performance overhead due to protocol translation
- Limited support for some gRPC features in REST endpoints
Code Comparison
grpc:
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
grpc-gateway:
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {
option (google.api.http) = {
post: "/v1/hello"
body: "*"
};
}
}
The grpc-gateway example includes additional annotations to define RESTful endpoints, while the grpc example focuses solely on the gRPC service definition.
grpc is the core implementation of gRPC, providing high-performance RPC capabilities. grpc-gateway extends this functionality by offering a bridge between gRPC and RESTful APIs, making it easier to integrate gRPC services into existing REST-based ecosystems. While grpc-gateway adds flexibility, it also introduces additional complexity and potential performance overhead. The choice between the two depends on specific project requirements and the need for REST compatibility.
The official Go library for the Cloudflare API
Pros of cloudflare-go
- Specifically designed for Cloudflare API interactions
- Simpler to use for Cloudflare-related tasks
- Lighter weight and more focused in scope
Cons of cloudflare-go
- Limited to Cloudflare-specific functionality
- Smaller community and fewer contributors
- Less extensive documentation compared to gRPC
Code Comparison
cloudflare-go example:
api, err := cloudflare.New(apiKey, user)
zoneID, err := api.ZoneIDByName(domain)
err = api.UpdateZoneSSLSettings(zoneID, cloudflare.ZoneSSLSetting{Value: "full"})
gRPC example:
conn, err := grpc.Dial(address, grpc.WithInsecure())
c := pb.NewGreeterClient(conn)
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
While both libraries are written in Go, they serve different purposes. cloudflare-go is tailored for interacting with Cloudflare's API, making it easier to manage Cloudflare resources. gRPC, on the other hand, is a general-purpose RPC framework that can be used for various types of client-server communication across different languages and platforms.
Apache Thrift
Pros of Thrift
- Supports a wider range of programming languages (20+)
- More flexible serialization options (binary, compact, JSON)
- Simpler to set up and use for smaller projects
Cons of Thrift
- Less active development and community support
- Lacks built-in features like authentication and load balancing
- Performance may be slower compared to gRPC, especially for large-scale systems
Code Comparison
Thrift IDL:
service Calculator {
i32 add(1: i32 num1, 2: i32 num2)
}
gRPC Protocol Buffer:
service Calculator {
rpc Add(AddRequest) returns (AddResponse) {}
}
message AddRequest {
int32 num1 = 1;
int32 num2 = 2;
}
message AddResponse {
int32 result = 1;
}
Both gRPC and Thrift are popular frameworks for building distributed systems and microservices. gRPC, developed by Google, offers better performance, stronger typing, and more advanced features like streaming and bidirectional communication. It's well-suited for large-scale, high-performance systems. Thrift, originally developed by Facebook and now an Apache project, provides broader language support and simpler setup, making it a good choice for smaller projects or those requiring support for less common programming languages.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
gRPC â An RPC library and framework
gRPC is a modern, open source, high-performance remote procedure call (RPC) framework that can run anywhere. gRPC enables client and server applications to communicate transparently, and simplifies the building of connected systems.
Homepage: | grpc.io |
Mailing List: | grpc-io@googlegroups.com |
To start using gRPC
To maximize usability, gRPC supports the standard method for adding dependencies to a user's chosen language (if there is one). In most languages, the gRPC runtime comes as a package available in a user's language package manager.
For instructions on how to use the language-specific gRPC runtime for a project, please refer to these documents
- C++: follow the instructions under the
src/cpp
directory - C#/.NET: NuGet packages
Grpc.Net.Client
,Grpc.AspNetCore.Server
- Dart: pub package
grpc
- Go:
go get google.golang.org/grpc
- Java: Use JARs from Maven Central Repository
- Kotlin: Use JARs from Maven Central Repository
- Node:
npm install @grpc/grpc-js
- Objective-C: Add
gRPC-ProtoRPC
dependency to podspec - PHP:
pecl install grpc
- Python:
pip install grpcio
- Ruby:
gem install grpc
- WebJS: follow the grpc-web instructions
Per-language quickstart guides and tutorials can be found in the documentation section on the grpc.io website. Code examples are available in the examples directory.
Precompiled bleeding-edge package builds of gRPC master
branch's HEAD
are
uploaded daily to packages.grpc.io.
To start developing gRPC
Contributions are welcome!
Please read How to contribute which will guide you through the entire workflow of how to build the source code, how to run the tests, and how to contribute changes to the gRPC codebase. The "How to contribute" document also contains info on how the contribution process works and contains best practices for creating contributions.
Troubleshooting
Sometimes things go wrong. Please check out the Troubleshooting guide if you are experiencing issues with gRPC.
Performance
See the Performance dashboard for performance numbers of master branch daily builds.
Concepts
See gRPC Concepts
About This Repository
This repository contains source code for gRPC libraries implemented in multiple languages written on top of a shared C++ core library src/core.
Libraries in different languages may be in various states of development. We are seeking contributions for all of these libraries:
Language | Source |
---|---|
Shared C++ [core library] | src/core |
C++ | src/cpp |
Ruby | src/ruby |
Python | src/python |
PHP | src/php |
C# (core library based) | src/csharp |
Objective-C | src/objective-c |
Language | Source repo |
---|---|
Java | grpc-java |
Kotlin | grpc-kotlin |
Go | grpc-go |
NodeJS | grpc-node |
WebJS | grpc-web |
Dart | grpc-dart |
.NET (pure C# impl.) | grpc-dotnet |
Swift | grpc-swift |
Top Related Projects
Go support for Google's protocol buffers
A simple RPC framework with protobuf service definitions
gRPC to JSON proxy generator following the gRPC HTTP spec
The official Go library for the Cloudflare API
Apache Thrift
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot