GithubHelp home page GithubHelp logo

esoubiran-aneo / adonis-grpc-consumer Goto Github PK

View Code? Open in Web Editor NEW

This project forked from julien-r44/adonis-grpc-consumer

0.0 0.0 0.0 123 KB

๐Ÿ•ธ๏ธ Adonis gRPC client provider for easily communicate with gRPC services.

License: MIT License

JavaScript 2.53% TypeScript 97.47%

adonis-grpc-consumer's Introduction


Adonis gRPC Consumer

Communicate easily with gRPC services in Adonis

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 :

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 proto-loader-gen-types --longs=String --enums=String --defaults --oneofs --grpcLib=@grpc/grpc-js --outDir=./proto/ ./proto/*.proto

proto-loader-gen-types is a executable from @grpc/proto-loader package 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 :

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:

import GrpcConsumer, { grpc } from '@ioc:Adonis/Addons/GrpcConsumer'
import { MyServiceClient } from 'proto/my_service/MyService'

const client = GrpcConsumer.getClient<MyServiceClient>('MY_SERVICE')
client.SendMessage({ message: 'hello !' }, (error?: grpc.ServiceError) => {
    if (error) {
      console.error(error.message)
    }
  }
)

adonis-grpc-consumer's People

Contributors

julien-r44 avatar esoubiran-aneo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.