From ac19e71ce460faec0b6005613a89e569847f90fd Mon Sep 17 00:00:00 2001 From: AMontagu Date: Mon, 27 Nov 2023 20:13:12 +0100 Subject: [PATCH] first draw of making DSG work with browser --- docs/how-to/web.rst | 60 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/docs/how-to/web.rst b/docs/how-to/web.rst index 0ae0004a..bdd803b0 100644 --- a/docs/how-to/web.rst +++ b/docs/how-to/web.rst @@ -4,8 +4,60 @@ Web Description ----------- -Usage ------ +It's possible to use gRPC in a broswer considering some limitations (See `state-of-grpc-web `_. +Using it allow you to have only one interface for all your customer. It also enable server side streams that is not existing in classic REST protocols. -Example -------- +All the next step described here can be found in the `dsg-example repository `_ in the `frontend/grpc-web-example ` directory. + +We will use `BUF `_ to generate the JS doc. Note that grpc-web have it's own protoc-gen-grpc-web plugin to generate JS file but it `doesn't generate js ESM module `_ and it is `not compatible with Vite easily `_. + +It work the same as traditionnal gRPC except that a proxy is charged to transform traditional request into enforced HTTP/2 gRPC compatible one. + +Depending on the backend you use `you may even not need a proxy but just some middleware`_. But in Python it's still needed for now. + +If you are considering a production deployement on a kubernetes cluster you may consider the usage of `Istio `_ that produce a `grpc-web proxy out of the box `_ without any specific configuration. For local development follow next steps. + +The Envoy Proxy & docker image +----------- + +The default recommended proxy is `Envoy `_. The doc of `grpc-web `_ document how to use it and even give you a example config file: `envoy.yaml `_ + +In this example file the importante lines you need to know beacause you may need to change them are: +- l.10: specify the listening port +- l.60 & 61: specify the address and port of the grpc-server +- l.26 & 48: cluster name need to match together + +To help you understand how to launch it you can have a look in our example repository: +- `Envoy configuration and dockerfile `_ +- `Docker compose conf `_ + +This can also be launched in production environment but if the envoy proxy is not located in the same local network it can bring latency. Please consider using `Istio `_ if in kubernetes deployement + +Generating JS client +----------- + +By using BUF you can upload your proto files directly to `BSR `_ and use their SDK to `dynamically generate files while pushing to registry `_. + +But to help understand how it work and making simple example we will use `locally generated files `_ +Here is the step needed: + +- Install dependencies (3 in dev mode and 3 in normal mode ). `Example `_ +- Create the buf.gen.yaml with at least the es and the connect-es plugin. Even if it can be anywhere we recommend you to put it at the root of your js folder or your API folder. The example will only work if at the root of a vue vite/webpack project as it expect a src folder to exist. `Example `_ +- Copy the proto file into a proto directory created in the folder of the buf.gen.yaml file. `Example `_ +- Launch the command: npx buf generate proto `Explanation `_ +- A src/gen folder should create with two file. _connect.js file with the Services/Controllers file and _pb.js with request and response message file `Example `_ + +Once this two file are generated you are good to go to the next step + + +Using JS client +----------- + +BUF already documented this part: `Using clients `_. + +But there is some details that can be confusing: +- You need to use the `createGrpcWebTransport protocol _`. +- If proto was generated by DSG then the _connect.js file export Service name with Controller instead of Service name. In the BUF doc ElizaService should have been ElizaController +- If API field use snake_case they should be setted and getted by camelCase if using the createGrpcWebTransport as grpc-web automatically convert fields. + +See `our DSG example for more explicit example `_ \ No newline at end of file