easy-grpc-client

Easy gRPC client for node js

Usage no npm install needed!

<script type="module">
  import easyGrpcClient from 'https://cdn.skypack.dev/easy-grpc-client';
</script>

README

easy-grpc-client

Easy gRPC client for node JS

This NPM allows to make requests to an gRPC server

Methods

  • callService('methodName': string, data: any, callback: Function(error, result));
  • getMethods();

Dynamic methods

The dynamic methods will be created using the proto file metadata.

  • Example dynamic (using callbacks) method: method1(data, callback);.
  • Example dynamic stream method: method2Stream(data).on('data', callback);

Typed

How to use it

example.proto file

syntax = "proto3";

package my_package;

message RequestMessage {
    string name = 2;
}

message ResponseMessage {
    string greeting = 2;
}

service ServiceName {
    rpc Method1 (RequestMessage) returns (ResponseMessage);
    rpc Method2 (RequestMessage) returns (ResponseMessage);
}

Where

  • The name of the package is my_package.
  • The name of the service is ServiceName.
  • The name of the methods are Method1 and Method2.

Load client


import * as EasyGrpc from 'easy-grpc-client'

...

const grpcClient = new EasyGrpcClient(<protoFilePath>, <packageName>, <serviceName>, <url>);

Usage example

import { join } from 'path';
import * as MSTypes from 'ms-types/lib';
import * as EasyGrpc from 'easy-grpc-client';

const PROTO_PACKAGE = 'my_package';
const PROTO_SERVICE_NAME = 'ServiceName';

export class MGConnector {
  private myClient: EasyGrpc.EasyGrpcClient;
  private service: MSTypes.IMg;

  constructor(url: string) {
    const PROTO_PATH = join(__dirname, './example.proto');
    this.myClient = new EasyGrpc.EasyGrpcClient(
      PROTO_PATH,
      PROTO_PACKAGE,
      PROTO_SERVICE_NAME,
      url,
    );
    this.service = this.myClient.getService<MSTypes.IMg>();
  }

  async sendEmail($in: MSTypes.IRequestMessage): Promise<MSTypes.IResponseMessage> {
    return this.service.Method1($in);
  }

  async getStatus($in: MSTypes.IRequestMessage): Promise<MSTypes.IResponseMessage> {
    return this.service.Method2($in);
  }
}

Author

Contributors