https://github.com/drgomesp/go-libp2p-grpc
⚙ gRPC/Protobuf on Libp2p with gRPC-Gateway support
https://github.com/drgomesp/go-libp2p-grpc
go go-libp2p grpc grpc-go libp2p libp2p-gorpc libp2p-transport
Last synced: 8 months ago
JSON representation
⚙ gRPC/Protobuf on Libp2p with gRPC-Gateway support
- Host: GitHub
- URL: https://github.com/drgomesp/go-libp2p-grpc
- Owner: drgomesp
- License: mit
- Created: 2022-09-04T13:33:05.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2023-08-15T01:51:59.000Z (almost 3 years ago)
- Last Synced: 2025-03-24T17:21:23.497Z (over 1 year ago)
- Topics: go, go-libp2p, grpc, grpc-go, libp2p, libp2p-gorpc, libp2p-transport
- Language: Go
- Homepage:
- Size: 15.7 MB
- Stars: 8
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-libp2p-grpc
[](https://github.com/drgomesp/)
[](https://goreportcard.com/report/github.com/drgomesp/go-libp2p-grpc)
[](https://github.com/drgomesp/go-libp2p-grpc/actions)
[](https://codecov.io/gh/drgomesp/go-libp2p-grpc)
> ⚙ gRPC/Protobuf on Libp2p with gRPC-Gateway support.
## Table of Contents
- [Install](#install)
- [Features](#features)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)
## Install
```bash
go get github.com/drgomesp/go-libp2p-grpc
```
## Features
- [x] **[GRPC/Protobuf](https://grpc.io/docs/languages/go/)** support for any Libp2p **[`host.Host`](https://github.com/libp2p/go-libp2p/blob/master/core/host/host.go#L25)**
- [x] **[GRPC Gateway](https://grpc-ecosystem.github.io/grpc-gateway/)** and **[OpenAPI/Swagger](https://swagger.io/specification/v2/)** support through **[`go-libp2p-http`](https://github.com/libp2p/)**
## Usage
> For a working example with gRPC-gateway support, check the **[examples/](https://github.com/drgomesp/go-libp2p-grpc/tree/main/examples)** folder.
Given an RPC service:
```proto
service EchoService {
// Echo asks a node to respond with a message.
rpc Echo(EchoRequest) returns (EchoReply) {}
}
```
```go
type EchoService struct {}
func (EchoService) Echo(context.Context, *EchoRequest) (*EchoReply, error) {
...
}
```
And a libp2p host to act as the server:
```go
ma, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10000")
serverHost, err := libp2p.New(libp2p.ListenAddrs(ma))
if err != nil {
log.Fatal(err)
}
defer serverHost.Close()
srv, err := libp2pgrpc.NewGrpcServer(ctx, serverHost)
if err != nil {
log.Fatal(err)
}
```
Register the gRPC service to the host server:
```go
pb.RegisterEchoServiceServer(srv, &EchoService{})
```
Start gRPC for serve:
```go
go srv.Serve()
```
A libp2p host to act as the client:
```go
ma, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10001")
clientHost, err := libp2p.New(libp2p.ListenAddrs(ma))
if err != nil {
log.Fatal(err)
}
```
Dial the server and initiate the request:
```go
client := libp2pgrpc.NewClient(cliHost, libp2pgrpc.ProtocolID, libp2pgrpc.WithServer(srv))
conn, err := client.Dial(ctx, serverHost.ID(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatal(err)
}
defer conn.Close()
c := pb.NewEchoServiceClient(conn)
res, err := c.Echo(ctx, &pb.EchoRequest{Message: "give me something"})
```
## Contributing
PRs accepted.
## License
MIT © [Daniel Ribeiro](https://github.com/drgomesp)