Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Julien-R44/adonis-grpc-consumer
πΈοΈ Adonis gRPC client provider for easily communicate with gRPC services.
https://github.com/Julien-R44/adonis-grpc-consumer
adonisjs grpc grpc-client
Last synced: about 1 month ago
JSON representation
πΈοΈ Adonis gRPC client provider for easily communicate with gRPC services.
- Host: GitHub
- URL: https://github.com/Julien-R44/adonis-grpc-consumer
- Owner: Julien-R44
- License: mit
- Created: 2021-12-21T15:49:09.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-29T13:31:52.000Z (over 2 years ago)
- Last Synced: 2024-10-02T03:18:40.640Z (3 months ago)
- Topics: adonisjs, grpc, grpc-client
- Language: TypeScript
- Homepage:
- Size: 243 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
- awesome-adonisjs - Adonis GRPC Consumer - gRPC client provider for easily communicate with multiples gRPC services. (Packages)
README
## Installation
```
npm i adonis-grpc-consumer
node ace configure adonis-grpc-consumer
```## Usage Example
First of all, you need to create a "proto" folder at the root of your Adonis project in which you will obviously store your protobuf definition files.
my-app/proto/myService.proto :
```protobuf
syntax = "proto3";
package my_service;message Empty {}
service MyService {
rpc SendMessage (SendMessageRequest) returns (Empty) {};
}message SendMessageRequest {
string message = 1;
}
```Now you have to generate the type definitions for typescript. To do this, run :
```
npx build-proto --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=./proto/ ./proto/*.proto
```
`build-proto` is an executable from `@grpc/proto-loader` package ( `proto-loader-gen-types` ) that is embedded in `adonis-grpc-consumer`.If everything went well, in my-app/proto/ you should find your TS definition files next to your .proto file.
Now we go back to Adonis, we will add our freshly created service as a consumable service, in config/grpc-consumer.ts :
```typescript
let grpcConfig: GrpcConsumerConfig = {
verbose: true,
clients: [
{
name: 'MY_SERVICE',
options: {
package: 'my_service',
serviceName: 'MyService',
protoPath: path.join(__dirname + '/../proto/myService.proto'),
url: '127.0.0.1:4545', // Don't forget to add your service url here
},
},
],
}export default grpcConfig
```Try to launch your application, in case everything went well, you should see the following message (only with `verbose: true`):
```
[GRPC] Client MY_SERVICE connected !
```To use our service and call the `SendMessage` function defined in the protobuf file, we do the following:
```typescript
import GrpcConsumer, { grpc } from '@ioc:Adonis/Addons/GrpcConsumer'
import { MyServiceClient } from 'proto/my_service/MyService'const client = GrpcConsumer.getClient('MY_SERVICE')
client.SendMessage({ message: 'hello !' }, (error?: grpc.ServiceError) => {
if (error) {
console.error(error.message)
}
}
)
```