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.
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.
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…
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.
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.
After spending sometime i was able to create proto files for typescript by following steps:
npm install google-protobuf @types/google-protobuf grpc-web-client ts-protoc-gen --save
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
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
grpc/grpc-web is available at https://github.com/grpc/grpc-web
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With