GRPC Bus
GRPC-bus is a mechanism to call GRPC services from the browser using a Node.JS server as a proxy. The link between the browser and Node.JS is defined by the user, but could be something like a WebSocket.
The server and client are expected to share the same / similar protobuf tree. For example, the same result should come from the following code on both the client and server:
root;
In this way the client can implement the ProtoBuf.JS RPC interfaces in the browser. Then, the grpc-bus package does the following:
- Keep track of connections to desired remote servers
- Make service calls on behalf of the client
- Keep track of streaming calls and pipe these back to the client accordingly.
Thus, we can call GRPC servers from the browser via a Node.JS websocket stream.
Example
First, create your client, and give it a way to communicate with the server:
var protoRoot = ProtobufJS;var grpcBus = ; // MySendFunction takes a message object.// This message should be passed to handleMessage on the server.var client = protoTree mySendFunction; var tree = clientroot;tree'localhost:3000';
You should always call service.end()
when you are done with a service handle, so the server knows it's safe to dispose it.
You'll notice that inside the then
block the API is exactly the same as the Node GRPC api.