Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GRPC web client with angular 6

I have worked with grpc .net client and a grpc server created with java, how can i implement grpc web client on angular 6 with typescript? Also how can i create proto files and it's typing's for typescript? I am following this repo but not able to generate proto files.

like image 853
Asad Shah Avatar asked Aug 15 '18 10:08

Asad Shah


People also ask

Can gRPC be used with angular?

You may need to use JWT authentication with your grpc applications that can be easily implemented. All you need is to add the [Authorize] attribute on the server-side. Then, you can add your token from Metadata section in Kreya. Adding your token into the metadata section will be enough for Angular.

Is gRPC good for Web?

gRPC-Web is an extension to gRPC which makes it compatible with browser-based code (technically, it's a way of doing gRPC over HTTP/1.1 requests). gRPC-Web hasn't become prevalent yet because not many server or client frameworks have offered support for it…

Can I use gRPC in browser?

gRPC-Web allows browser apps to call gRPC services with the gRPC-Web client and Protobuf. Similar to normal gRPC, but it has a slightly different wire-protocol, which makes it compatible with HTTP/1.1 and browsers. Requires the browser app to generate a gRPC client from a . proto file.

Is gRPC-Web production ready?

With gRPC-Web, you can now easily build truly end-to-end gRPC application architectures by defining your client and server-side data types and service interfaces with Protocol Buffers. This has been a hotly requested feature for a while, and we are finally happy to say that it is now ready for production use.


4 Answers

After spending sometime i was able to create proto files for typescript by following steps:

  1. Download protobuf for windows from this link. After extracting files set the path variable for protoc.exe
  2. install npm packages npm install google-protobuf @types/google-protobuf grpc-web-client ts-protoc-gen --save
  3. After installing it generate the typescript files by using the command: protoc --plugin="protoc-gen-ts=absolute-path-to-your-project\node_modules\.bin\protoc-gen-ts.cmd" --js_out="import_style=commonjs,binary:${OUT_DIR}" --ts_out="service=true:${OUT_DIR}" your.proto
  4. Finally consume it like as mentioned in this repo.
like image 53
Asad Shah Avatar answered Sep 28 '22 03:09

Asad Shah


You can also get it to work by running:

npm install google-protobuf protoc ts-protoc-gen

Then add a compile script to your package.json:

"compile": "./node_modules/protoc/protoc/bin/protoc --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:src/app/proto-gen --ts_out=service=true:src/app/proto -I ./src/app/proto ./src/app/proto/**/*.proto",

now you can compile the .proto files to a Service using:

npm run compile

You can see the approach working here:

https://github.com/kmturley/angular-nest-grpc

like image 26
Kim T Avatar answered Sep 28 '22 05:09

Kim T


grpc/grpc-web is available at https://github.com/grpc/grpc-web

like image 21
Wenbo Zhu Avatar answered Sep 28 '22 04:09

Wenbo Zhu


Cannot use on on Windows

"compile": "./node_modules/protoc/protoc/bin/protoc
--plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:src/app/proto-gen --ts_out=service=true:src/app/proto -I ./src/app/proto ./src/app/proto/**/*.proto",

I get '.' is not recognized as an internal or external command

This works fine: package.json

"scripts": {
"compile": "node_modules\\protoc\\protoc\\bin\\protoc --plugin=protoc-gen-ts=node_modules\\.bin\\protoc-gen-ts.cmd --js_out=import_style=commonjs,binary:./proto/generated --ts_out=service=grpc-web:./proto/generated -I ./proto ./proto/*.proto"
}

npm run compile

like image 38
Tomáš Moskala Avatar answered Sep 28 '22 04:09

Tomáš Moskala