How to bring a gRPC defined API to the web browser

前端 未结 8 769
孤街浪徒
孤街浪徒 2021-01-29 22:39

We want to build a Javascript/HTML gui for our gRPC-microservices. Since gRPC is not supported on the browser side, we thought of using web-sockets to connect to a node.js serve

8条回答
  •  感情败类
    2021-01-29 23:27

    GRPC Bus WebSocket Proxy does exactly this by proxying all GRPC calls over a WebSocket connection to give you something that looks very similar to the Node GRPC API in the browser. Unlike GRPC-Gateway, it works with both streaming requests and streaming responses, as well as non-streaming calls.

    There is both a server and client component. The GRPC Bus WebSocket Proxy server can be run with Docker by doing docker run gabrielgrant/grpc-bus-websocket-proxy

    On the browser side, you'll need to install the GRPC Bus WebSocket Proxy client with npm install grpc-bus-websocket-client

    and then create a new GBC object with: new GBC(, , )

    For example:

    var GBC = require("grpc-bus-websocket-client");
    
    new GBC("ws://localhost:8080/", 'helloworld.proto', {helloworld: {Greeter: 'localhost:50051'}})
      .connect()
      .then(function(gbc) {
        gbc.services.helloworld.Greeter.sayHello({name: 'Gabriel'}, function(err, res){
          console.log(res);
        });  // --> Hello Gabriel
      });
    

    The client library expects to be able to download the .proto file with an AJAX request. The service-map provides the URLs of the different services defined in your proto file as seen by the proxy server.

    For more details, see the GRPC Bus WebSocket Proxy client README

提交回复
热议问题