Giter Site home page Giter Site logo

grpc-web-go-client's Introduction

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)
}

grpc-web-go-client's People

Contributors

code-hex avatar ktr0731 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

grpc-web-go-client's Issues

Complete documentation?

Is there any documentation about how can we connect to a grpcweb server ?
There is something in .md file but i think its incomplete?

Trailer parsing requires whitespace after the colon separating key and value

The gRPC-Web protocol specifies the trailer is encoded as "Key-value pairs encoded as a HTTP/1 headers block (without the terminating newline), per https://tools.ietf.org/html/rfc7230#section-3.2" in the last message in the body of the response.

https://tools.ietf.org/html/rfc7230#section-3.2 specifies that the whitespace between the colon and the field value should be optional. However, the code at https://github.com/ktr0731/grpc-web-go-client/blob/master/grpcweb/parser/parser.go#L82 requires that there is exactly one space of whitespace between the colon and the field value.

This causes the following error when using evans (which is really nice by the way, thanks!) with a server that does not leave a space between the colon and the field value:

helloworld.GreeterService@localhost:8081> call SayHello
name (TYPE_STRING) => asdf
command call: failed to send a request: grpc-web: failed to send a request: failed to parse status and trailer: unexpected EOF

helloworld.GreeterService@localhost:8081> 

evans installing fails because of grpc-web-go-client

When I tried to install evans, I got this message.

~ # go get github.com/ktr0731/evans
# github.com/ktr0731/grpc-web-go-client/grpcweb/grpcweb_reflection_v1alpha
/go/src/github.com/ktr0731/grpc-web-go-client/grpcweb/grpcweb_reflection_v1alpha/reflection.go:31:30: multiple-value c.cc.BidiStreaming() in single-value context

You changed client.BidiStreaming couple days ago
46586e1#diff-7e67c0929808c88b228892ce33bcc737R289

But here, still get single return variable.

Don't you forget update this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.