Convert Figma logo to code with AI

twitchtv logotwirp

A simple RPC framework with protobuf service definitions

7,115
326
7,115
7

Top Related Projects

20,846

The Go language implementation of gRPC. HTTP/2 based RPC

[Deprecated] Protocol Buffers for Go with Gadgets

21,795

A Go microservices framework

26,478

A standard library for microservices.

Quick Overview

Twirp is a simple RPC framework built on Protocol Buffers, developed by Twitch. It's designed to be a simpler alternative to gRPC, focusing on ease of use and minimal dependencies. Twirp generates Go code from Protobuf definitions and supports both JSON and Protobuf serialization.

Pros

  • Simple and lightweight compared to gRPC
  • Easy to set up and use with minimal boilerplate
  • Supports both JSON and Protobuf serialization
  • Generates client and server code automatically

Cons

  • Limited language support (primarily Go, with some community-driven support for other languages)
  • Fewer features compared to more comprehensive RPC frameworks like gRPC
  • Less widespread adoption and community support compared to alternatives

Code Examples

  1. Defining a service in Protobuf:
syntax = "proto3";

package example;

service Haberdasher {
  rpc MakeHat(Size) returns (Hat) {}
}

message Size {
  int32 inches = 1;
}

message Hat {
  int32 inches = 1;
  string color = 2;
  string name = 3;
}
  1. Implementing the server in Go:
package main

import (
    "context"
    "net/http"
    pb "example/haberdasher"
)

type HaberdasherServer struct{}

func (s *HaberdasherServer) MakeHat(ctx context.Context, size *pb.Size) (*pb.Hat, error) {
    return &pb.Hat{
        Inches: size.Inches,
        Color:  "red",
        Name:   "fedora",
    }, nil
}

func main() {
    server := &HaberdasherServer{}
    twirpHandler := pb.NewHaberdasherServer(server)
    http.ListenAndServe(":8080", twirpHandler)
}
  1. Using the generated client in Go:
package main

import (
    "context"
    "net/http"
    pb "example/haberdasher"
)

func main() {
    client := pb.NewHaberdasherProtobufClient("http://localhost:8080", &http.Client{})
    hat, err := client.MakeHat(context.Background(), &pb.Size{Inches: 12})
    if err != nil {
        // Handle error
    }
    // Use hat
}

Getting Started

  1. Install the Twirp protoc plugin:

    go install github.com/twitchtv/twirp/protoc-gen-twirp@latest
    
  2. Define your service in a .proto file

  3. Generate Go code:

    protoc --twirp_out=. --go_out=. your_service.proto
    
  4. Implement the generated server interface and use the generated client in your Go code

  5. Run your server and make client requests

Competitor Comparisons

20,846

The Go language implementation of gRPC. HTTP/2 based RPC

Pros of grpc-go

  • More feature-rich and mature ecosystem
  • Better performance for large-scale, high-throughput systems
  • Supports bidirectional streaming and server-side streaming

Cons of grpc-go

  • Steeper learning curve and more complex implementation
  • Requires HTTP/2 and binary protocol, which can be challenging for debugging
  • Heavier dependency footprint

Code Comparison

grpc-go:

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

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

Twirp:

service Haberdasher {
  rpc MakeHat(Size) returns (Hat);
}

message Size {
  int32 inches = 1;
}

message Hat {
  int32 inches = 1;
  string color = 2;
  string name = 3;
}

Both frameworks use Protocol Buffers for service definitions, but Twirp's implementation is generally simpler and more lightweight. grpc-go offers more advanced features and flexibility, while Twirp focuses on ease of use and HTTP compatibility. The choice between the two depends on project requirements, scalability needs, and developer preferences.

[Deprecated] Protocol Buffers for Go with Gadgets

Pros of gogo/protobuf

  • Higher performance and smaller code size due to optimized serialization/deserialization
  • Extended set of Protocol Buffers features and customization options
  • Better compatibility with Go idioms and standard library

Cons of gogo/protobuf

  • More complex setup and configuration compared to Twirp's simplicity
  • Potential compatibility issues with standard protobuf implementations
  • Steeper learning curve for developers new to Protocol Buffers

Code Comparison

gogo/protobuf:

import "github.com/gogo/protobuf/proto"

data, err := proto.Marshal(&myMessage)
err = proto.Unmarshal(data, &myMessage)

Twirp:

import "github.com/twitchtv/twirp"

client := NewHaberdasherProtobufClient("http://localhost:8080", &http.Client{})
hat, err := client.MakeHat(ctx, &Size{Inches: 12})

Key Differences

  • gogo/protobuf focuses on optimizing Protocol Buffers for Go, while Twirp is a RPC framework
  • Twirp provides a simpler, more opinionated approach to building services
  • gogo/protobuf offers more flexibility and performance optimizations for Protocol Buffers
  • Twirp includes built-in HTTP transport and routing, whereas gogo/protobuf is primarily for serialization

Both projects serve different purposes but can be complementary in certain scenarios, with gogo/protobuf potentially being used for message serialization within a Twirp-based service.

21,795

A Go microservices framework

Pros of go-micro

  • More comprehensive microservices framework with additional features like service discovery, load balancing, and distributed tracing
  • Supports multiple transport protocols (gRPC, HTTP, NATS, etc.)
  • Extensible plugin system for customization and additional functionality

Cons of go-micro

  • Steeper learning curve due to its more complex architecture and feature set
  • Potentially heavier resource usage compared to Twirp's lightweight approach
  • May introduce unnecessary complexity for simpler microservices projects

Code Comparison

go-micro example:

service := micro.NewService(
    micro.Name("greeter"),
    micro.Version("latest"),
)
service.Init()

Twirp example:

server := &twirp.Server{
    ProtobufHandler: NewHaberdasherServer(myHaberdasher),
}
http.ListenAndServe(":8080", server)

Both frameworks aim to simplify microservices development in Go, but go-micro offers a more feature-rich ecosystem at the cost of increased complexity. Twirp, on the other hand, provides a simpler, lightweight approach focused on RPC communication. The choice between the two depends on the specific requirements of your project and the desired level of abstraction and features needed for your microservices architecture.

26,478

A standard library for microservices.

Pros of kit

  • More comprehensive and feature-rich, offering a complete toolkit for microservices
  • Provides support for various transport protocols (HTTP, gRPC, AMQP)
  • Offers middleware, service discovery, and load balancing capabilities

Cons of kit

  • Steeper learning curve due to its extensive feature set
  • More complex setup and configuration compared to Twirp
  • Potentially overkill for simpler microservice architectures

Code Comparison

kit:

type Service interface {
    Uppercase(string) (string, error)
}

func makeUppercaseEndpoint(svc Service) endpoint.Endpoint {
    return func(ctx context.Context, request interface{}) (interface{}, error) {
        req := request.(uppercaseRequest)
        v, err := svc.Uppercase(req.S)
        return uppercaseResponse{v}, err
    }
}

Twirp:

service Haberdasher {
    rpc MakeHat(Size) returns (Hat);
}

type Server struct{}

func (s *Server) MakeHat(ctx context.Context, size *Size) (*Hat, error) {
    return &Hat{Size: size.Inches, Color: "red", Name: "fedora"}, nil
}

kit offers more flexibility and control over service implementation, while Twirp provides a simpler, more straightforward approach to defining and implementing services.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Twirp Logo Build Status Go Report Card GoDoc


Twirp is a framework for service-to-service communication emphasizing simplicity and minimalism. It generates routing and serialization from API definition files and lets you focus on your application's logic instead of thinking about folderol like HTTP methods and paths and JSON.

Twirp is similar to gRPC, but without the custom HTTP server and transport implementations: it runs on the standard library's extremely-well-tested-and-high-performance net/http Server. It can run on HTTP 1.1, not just http/2, and supports JSON serialization for easy debugging.

Along the way, you get autogenerated clients and a simple, smart framework for passing error messages. Nice!

Read more about the motivation behind on the announcement blog post.

Documentation

Implementations in other languages

This repo contains the generator and runtime library for the Go implementation.

Here is a list of some third-party implementations in other languages.

LanguageClientsServersRepository
Crystal✓✓github.com/mloughran/twirp.cr
Dart✓github.com/apptreesoftware/protoc-gen-twirp_dart
Elixir✓✓github.com/keathley/twirp-elixir
Java✓✓github.com/fajran/protoc-gen-twirp_java_jaxrs
Java✓github.com/devork/flit
Java✓github.com/github/flit
JavaScript✓github.com/thechriswalker/protoc-gen-twirp_js
JavaScript✓github.com/Xe/twirp-codegens/cmd/protoc-gen-twirp_jsbrowser
JavaScript✓✓github.com/tatethurston/TwirpScript
Kotlin✓github.com/collectiveidea/twirp-kmm
PHP✓✓github.com/twirphp/twirp
Python3✓✓github.com/verloop/twirpy
Ruby✓✓github.com/twitchtv/twirp-ruby
Rust✓✓github.com/sourcefrog/prost-twirp
Scala✓✓github.com/soundcloud/twinagle
Swagger✓✓github.com/go-bridget/twirp-swagger-gen
Swift✓github.com/CrazyHulk/protoc-gen-swiftwirp
Typescript✓✓github.com/hopin-team/twirp-ts
Typescript✓✓github.com/tatethurston/TwirpScript
Typescript✓✓github.com/timostamm/protobuf-ts

Support and Community

We have a channel on the Gophers slack, #twirp, which is the best place to get quick answers to your questions. You can join the Gopher slack here.

Releases

Twirp follows semantic versioning through git tags, and uses GitHub Releases for release notes and upgrade guides: Twirp Releases

Contributing

Check out CONTRIBUTING.md for notes on making contributions.

License

This library is licensed under the Apache 2.0 License.