npm

Check out our latest tech talk, "JavaScript Supply Chain Security" presented by VP of Security, Adam Baldwin.Watch it here »

node-grpc-client

1.3.0 • Public • Published

node-grpc-client

Simple gRPC client on NodeJS v1.3.0

Methods

  • runService('TaskName', dataToSend, callback);
  • listMethods();

Dynamic methods

The dynamic methods are the methods of the GRPC, then, its depends of your definition on your proto file.

  • Example dynamic async (using callbacks) method: task1Async(data, callback);.
  • Example dynamic sync (using promises) method: await task2Sync(data);.
  • Example dynamic stream method: task3Stream(data).on('data', callback);

How to use it

example.proto file

syntax = "proto3";
 
package packageservice;
 
message ExampleRequest {
    string id = 1;
    string text = 2;
}
 
message ExampleResponse {
    int32 send = 1;
    string message = 2;
}
 
service Theservice {
    rpc Task1 (ExampleRequest) returns (ExampleResponse);
    rpc Task2 (ExampleRequest) returns (ExampleResponse);
}

Where

  • The name of the package is packageservice.
  • The name of the service is Theservice.
  • The name of the methods are Task1 and Task2.

Load client

 
const GRPCClient = require('node-grpc-client');
 
const myClient = new GRPCClient(<protoPath>, <packageName>, <serviceName>, <url>);
 

Full example using runService method

 
const path = require('path');
const PROTO_PATH = path.resolve(__dirname, '../example.proto');
 
const GRPCClient = require('node-grpc-client');
 
const myClient = new GRPCClient(PROTO_PATH, 'packageservice', 'Theservice', 'localhost:3000');
 
const dataToSend = {
    id: 'abc123',
    text: 'Hello world'
};
 
myClient.runService('Task1', dataToSend, (err, res) => {
 
    console.log('Service response ', res);
 
});
 

Full example using dynamic async (using callbacks) methods

 
const path = require('path');
const PROTO_PATH = path.resolve(__dirname, '../example.proto');
 
const GRPCClient = require('node-grpc-client');
 
const myClient = new GRPCClient(PROTO_PATH, 'packageservice', 'Theservice', 'localhost:3000');
 
const dataToSend = {
    id: 'abc123',
    text: 'Hello world'
};
 
myClient.task1Async(dataToSend, (err, res) => {
 
    console.log('Service response ', res);
 
});
 

Full example using dynamic sync (using promises) methods

 
const path = require('path');
const PROTO_PATH = path.resolve(__dirname, '../example.proto');
 
const GRPCClient = require('node-grpc-client');
 
const myClient = new GRPCClient(PROTO_PATH, 'packageservice', 'Theservice', 'localhost:3000');
 
const dataToSend = {
    id: 'abc123',
    text: 'Hello world'
};
 
(async function () {
 
    const response1 = await myClient.task1Sync(dataToSend);
    console.log('The answer of request 1: ', response1);
    const response2 = await myClient.task2Sync(dataToSend);
    console.log('The answer of request 2: ', response2);
 
})();
 

Streaming gRPC

Some gRPC methods return a stream. This allows subscription-based push communication between the server and the client, where the server can push data to the client.

You can use the dynamic stream method for this. For example, if the server has a gRPC method Task3 that returns a stream:

 
const stream = myClient.task3Stream(dataToSend);
stream.on('data'(data) => console.log(data));
 

Authors

Contributors

install

npm i node-grpc-client

Downloadsweekly downloads

177

version

1.3.0

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability