Socket
Socket
Sign inDemoInstall

github.com/ktr0731/grpc-web-go-client

Package Overview
Dependencies
10
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    github.com/ktr0731/grpc-web-go-client


Version published

Readme

Source

gRPC Web Go client

GoDoc GitHub Actions

THE IMPLEMENTATION IS LACKING

gRPC Web client written in Go.

Usage

The server is here.

Send an unary request.

client := grpcweb.NewClient("localhost:50051")

in, out := new(api.SimpleRequest), new(api.SimpleResponse)
in.Name = "ktr"

// You can get the endpoint from grpcweb.ToEndpoint function with descriptors.
// However, I write directly in this example.
req := grpcweb.NewRequest("/api.Example/Unary", in, out)

res, err := client.Unary(context.Background(), req)
if err != nil {
  log.Fatal(err)
}

// hello, ktr
fmt.Println(res.Content.(*api.SimpleResponse).GetMessage())

Send a server-side streaming request.

req := grpcweb.NewRequest("/api.Example/ServerStreaming", in, out)

stream, err := client.ServerStreaming(context.Background(), req)
if err != nil {
  log.Fatal(err)
}

for {
  res, err := stream.Receive()
  if err == io.EOF {
    break
  }
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(res.Content.(*api.SimpleResponse).GetMessage())
}

Send an client-side streaming request.

stream, err := client.ClientStreaming(context.Background())
if err != nil {
  log.Fatal(err)
}

in, out := new(api.SimpleRequest), new(api.SimpleResponse)
in.Name = "ktr"
req := grpcweb.NewRequest("/api.Example/ClientStreaming", in, out)

for i := 0; i < 10; i++ {
  err := stream.Send(req)
  if err == io.EOF {
    break
  }
  if err != nil {
    log.Fatal(err)
  }
}

res, err := stream.CloseAndReceive()
if err != nil {
  log.Fatal(err)
}

// ktr, you greet 10 times.
fmt.Println(res.Content.(*api.SimpleResponse).GetMessage())

Send a bidirectional streaming request.

in, out := new(api.SimpleRequest), new(api.SimpleResponse)
req := grpcweb.NewRequest("/api.Example/BidiStreaming", in, out)

stream := client.BidiStreaming(context.Background(), req)

go func() {
  for {
    res, err := stream.Receive()
    if err == grpcweb.ErrConnectionClosed {
      return
    }
    if err != nil {
      log.Fatal(err)
    }
    fmt.Println(res.Content.(*api.SimpleResponse).GetMessage())
  }
}()

for i := 0; i < 2; i++ {
  in.Name = fmt.Sprintf("ktr%d", i+1)
  req := grpcweb.NewRequest("/api.Example/BidiStreaming", in, out)

  err := stream.Send(req)
  if err == io.EOF {
    break
  }
  if err != nil {
    log.Fatal(err)
  }
}

// wait a moment to get responses.
time.Sleep(10 * time.Second)

if err := stream.Close(); err != nil {
  log.Fatal(err)
}

FAQs

Last updated on 12 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc