modulepackage
0.1.0
Repository: https://github.com/drgomesp/go-libp2p-grpc.git
Documentation: pkg.go.dev
# README
go-libp2p-grpc
⚙ GRPC/Protobuf on Libp2p.
Table of Contents
Install
go get github.com/drgomesp/go-libp2p-grpc
Usage
Given an RPC service:
service EchoService {
// Echo asks a node to respond with a message.
rpc Echo(EchoRequest) returns (EchoReply) {}
}
type EchoService struct {}
func (EchoService) Echo(context.Context, *EchoRequest) (*EchoReply, error) {
...
}
And a libp2p host to act as the server:
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:
pb.RegisterEchoServiceServer(srv, &EchoService{})
A libp2p host to act as the client:
ma, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/10000")
clientHost, err := libp2p.New(libp2p.ListenAddrs(ma))
if err != nil {
log.Fatal(err)
}
Dial the server and initiate the request:
client := libp2pgrpc.NewClient(cliHost, libp2pgrpc.ProtocolID, libp2pgrpc.WithServer(srv))
conn, err := client.Dial(ctx, srvHost.ID(), grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, 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
# Functions
No description provided by the author
NewGrpcServer creates a Server object with the given LibP2P host and protocol.
No description provided by the author
# Variables
No description provided by the author
# Type aliases
ClientOption allows for functional setting of options on a Client.
ServerOption allows for functional setting of options on a Server.