npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-grpc-client

v1.5.0

Published

Simple gRPC client on NodeJS

Downloads

2,759

Readme

node-grpc-client

Simple gRPC client on NodeJS v1.5.0

* New feature in 1.5.0

Send metadata is supported.

* Important fix was solved in verison 1.4.0.

Now are supported packages which have dots or style of inverse DNS like: com.github.nodegrpcclient.

Methods

  • runService('TaskName', dataToSend, callback, options);
  • 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, options);.
  • Example dynamic sync (using promises) method: await task2Sync(data, options);.
  • Example dynamic stream method: task3Stream(data, options).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'
};

// options is optional and is supported from version 1.5.0
const options = {
    metadata: {
        hello: 'world'
    }
};

myClient.runService('Task1', dataToSend, (err, res) => {

    console.log('Service response ', res);

}, options);

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'
};

// options is optional and is supported from version 1.5.0
const options = {
    metadata: {
        hello: 'world'
    }
};

myClient.task1Async(dataToSend, (err, res) => {

    console.log('Service response ', res);

}, options);

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'
};

// options is optional and is supported from version 1.5.0
const options = {
    metadata: {
        hello: 'world'
    }
};

(async function () {

    const response1 = await myClient.task1Sync(dataToSend, options);
    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:


// options is optional and is supported from version 1.5.0
const options = {
    metadata: {
        hello: 'world'
    }
};

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

 


 

Options

Options is a feature added in version 1.5.0.

  • Currently support the property metadata.
  • Is optional, you can or can not send it like parameters in the methods.
  • In case is not passed like a parameter, its default value is {};

metadata

metadata is a property added to options in version 1.5.0.

  • This property has to be an object (key, value).
  • The values in most have to be a string. Just in case that the key ends with -bin, the values have to be a Buffer. Example:
const options = {
    metadata: {
        myheader: 'testing metadata', //string
        myheader2: '2', // string
        'myheader-bin': myBuffer // buffer
    }
}

 


 

Authors

Contributors