How to use grpc - 10 common examples

To help you get started, we’ve selected a few grpc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github grpc / grpc-node / test / stress / metrics_client.js View on Github external
*     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

'use strict';

// TODO(murgatroid99): use multiple grpc implementations
var grpc = require('grpc');

var proto = grpc.load(__dirname + '/../packages/grpc-native-core/ext/grpc/src/proto/grpc/testing/metrics.proto');
var metrics = proto.grpc.testing;

function main() {
  var parseArgs = require('minimist');
  var argv = parseArgs(process.argv, {
    string: 'metrics_server_address',
    boolean: 'total_only'
  });
  var client = new metrics.MetricsService(argv.metrics_server_address,
                                          grpc.credentials.createInsecure());
  if (argv.total_only) {
    client.getGauge({name: 'qps'}, function(err, data) {
      console.log(data.name + ':', data.long_value);
    });
  } else {
    var call = client.getAllGauges({});
github GoogleCloudPlatform / esp-v2 / tests / clients / agent_client.js View on Github external
var grpc = require('grpc');
var protoLoader = require('@grpc/proto-loader');

var PROTO_PATH = __dirname + '/../../api/agent/agent_service.proto';
var packageDefinition = protoLoader.loadSync(
  PROTO_PATH, {
    keepCase: true,
    longs: String,
    enums: String,
    defaults: true,
    oneofs: true,
    // includeDirs: [__dirname + '/proto/protobuf', __dirname + '/proto/googleapis'],
  });

var agent_proto = grpc.loadPackageDefinition(packageDefinition).google.api_proxy.agent;

var client = new agent_proto.AgentService('localhost:8790',
  grpc.credentials.createInsecure());


client.GetAccessToken({}, undefined, function(err, res) {
  if (err) {
    console.log("Error: ", err);
  } else {
    console.log("Result: ", res);
  }
});
github hyperledger / fabric-chaincode-node / test / unit / chaincode.js View on Github external
test('Chaincode command line arguments tests', (t) => {
	Chaincode = rewire('fabric-shim/lib/chaincode.js');
	let opts = Chaincode.__get__('opts');
	t.deepEqual(opts['peer.address'], undefined, 'Test zero argument');

	process.argv.push('--peer.address');
	process.argv.push('localhost:7051');
	delete require.cache[require.resolve('fabric-shim/lib/chaincode.js')];
	Chaincode = rewire('fabric-shim/lib/chaincode.js');
	opts = Chaincode.__get__('opts');
	t.deepEqual(opts['peer.address'], 'localhost:7051', 'Test passing only --peer.address argument is correctly picked up');
	t.equal(opts['grpc.max_send_message_length'], -1, 'Test grpc.max_send_message_length defaults to -1');
	t.equal(opts['grpc.max_receive_message_length'], -1, 'Test grpc.max_receive_message_length defaults to -1');
	t.equal(opts['grpc.keepalive_time_ms'], 110000, 'Test grpc.keepalive_time_ms defaults to 110000');
	t.equal(opts['grpc.http2.min_time_between_pings_ms'], 110000, 'Test grpc.http2.min_time_between_pings_ms defaults to 110000');
	t.equal(opts['grpc.keepalive_timeout_ms'], 20000, 'Test grpc.keepalive_timeout_ms defaults to 20000');
	t.equal(opts['grpc.http2.max_pings_without_data'], 0, 'Test grpc.http2.max_pings_without_data defaults to 0');
	t.equal(opts['grpc.keepalive_permit_without_calls'], 1, 'Test grpc.keepalive_permit_without_calls defaults to 1');

	process.argv.push('--test.another');
	process.argv.push('dummyValue9');
	delete require.cache[require.resolve('fabric-shim/lib/chaincode.js')];
	Chaincode = rewire('fabric-shim/lib/chaincode.js');
	opts = Chaincode.__get__('opts');
	t.deepEqual(opts['peer.address'], 'localhost:7051', 'Test passing two arguments and one is in the parsing definition');
	t.deepEqual(opts['test.another'], undefined, 'Test passing two arguments and one is NOT in the parsing definition');

	process.argv.pop();  // remove dummyValu9
	process.argv.pop();  // remove test.another

	process.argv.push('--grpc.max_send_message_length');
	process.argv.push('101');
github grpc / grpc-node / test / interop / interop_client.js View on Github external
function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
  // TODO(mlumish): enable TLS functionality
  var options = {};
  var creds;
  if (tls) {
    var ca_path;
    if (test_ca) {
      ca_path = path.join(__dirname, '../data/ca.pem');
      var ca_data = fs.readFileSync(ca_path);
      creds = grpc.credentials.createSsl(ca_data);
    } else {
      creds = grpc.credentials.createSsl();
    }
    if (host_override) {
      options['grpc.ssl_target_name_override'] = host_override;
      options['grpc.default_authority'] = host_override;
    }
  } else {
    creds = grpc.credentials.createInsecure();
  }
  var test = test_cases[test_case];

  var execute = function(err, creds) {
    assert.ifError(err);
    var client = new test.Client(address, creds, options);
    test.run(client, done, extra);
  };

  if (test.getCreds) {
    test.getCreds(extra.oauth_scope, function(err, new_creds) {
      assert.ifError(err);
      execute(err, grpc.credentials.combineChannelCredentials(
github grpc / grpc-node / test / interop / interop_client.js View on Github external
function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
  // TODO(mlumish): enable TLS functionality
  var options = {};
  var creds;
  if (tls) {
    var ca_path;
    if (test_ca) {
      ca_path = path.join(__dirname, '../data/ca.pem');
      var ca_data = fs.readFileSync(ca_path);
      creds = grpc.credentials.createSsl(ca_data);
    } else {
      creds = grpc.credentials.createSsl();
    }
    if (host_override) {
      options['grpc.ssl_target_name_override'] = host_override;
      options['grpc.default_authority'] = host_override;
    }
  } else {
    creds = grpc.credentials.createInsecure();
  }
  var test = test_cases[test_case];

  var execute = function(err, creds) {
    assert.ifError(err);
    var client = new test.Client(address, creds, options);
    test.run(client, done, extra);
  };

  if (test.getCreds) {
    test.getCreds(extra.oauth_scope, function(err, new_creds) {
      assert.ifError(err);
      execute(err, grpc.credentials.combineChannelCredentials(
github hyperledger / fabric-chaincode-node / test / unit / handler.js View on Github external
'ssl-target-name-override': 'dummyHost',
			'request-timeout': 12345,
			'another-property': 'dummyValue7',
			'key':'dummyKeyString',
			'cert':'dummyCertString',
			'grpc.max_send_message_length': 1,
			'grpc.max_receive_message_length': 2,
			'grpc.keepalive_time_ms': 3,
			'grpc.http2.min_time_between_pings_ms': 5,
			'grpc.keepalive_timeout_ms': 8,
			'grpc.http2.max_pings_without_data': 13,
			'grpc.keepalive_permit_without_calls': 21
		});

	t.equal(handler._options['grpc.ssl_target_name_override'], 'dummyHost', 'Test converting opts.ssl-target-name-override to grpc.ssl_target_name_override');
	t.equal(handler._options['grpc.default_authority'], 'dummyHost', 'Test converting opts.ssl-target-name-override to grpc.default_authority');
	t.equal(handler._options['request-timeout'], 12345, 'Test processing request-time option');
	t.equal(handler._options['another-property'], 'dummyValue7', 'Test processing another-property option');
	t.equal(handler._options['grpc.max_send_message_length'], 1, 'Test grpc.max_send_message_length set to 1');
	t.equal(handler._options['grpc.max_receive_message_length'], 2, 'Test grpc.max_receive_message_length set to 2');
	t.equal(handler._options['grpc.keepalive_time_ms'], 3, 'Test grpc.keepalive_time_ms set to 3');
	t.equal(handler._options['grpc.http2.min_time_between_pings_ms'], 5, 'Test grpc.http2.min_time_between_pings_ms set to 5');
	t.equal(handler._options['grpc.keepalive_timeout_ms'], 8, 'Test grpc.keepalive_timeout_ms set to 8');
	t.equal(handler._options['grpc.http2.max_pings_without_data'], 13, 'Test grpc.http2.max_pings_without_data set to 13');
	t.equal(handler._options['grpc.keepalive_permit_without_calls'], 21, 'Test grpc.keepalive_permit_without_calls set to 21');

	// The DNS can be case sensitive in some cases (eg Docker DNS)
	handler = new Handler(chaincodeObj, 'grpc://Peer.Example.com:7051');
	t.equal(handler._endpoint.addr, 'Peer.Example.com:7051', 'Test handler.addr value preserves casing');

	t.end();
});
github hyperledger / fabric-sdk-node / test / unit / remote.js View on Github external
t.equal(remote.getName(), aHost, 'checking the name assignment');
	t.equal(remote.toString(), ' Remote : {url:grpcs://atesthostname:9999}', 'Checking that peer.toString() reports correctly');
	t.equal(remote._grpc_wait_for_ready_timeout, 3000, 'Remote should have grpc waitForReady timeout default to 3000');
	t.ok(remote._endpoint.creds, 'GRPC Options tests: new Remote grpc with opts = null _endpoint.creds created');

	const dummy_name = 'areallygreatname';
	opts = {
		pem: aPem,
		'grpc.dummy_property': 'some_value',
		'ssl-target-name-override': aHostnameOverride,
		'grpc-wait-for-ready-timeout': 500,
		'name': dummy_name
	};
	remote = new Remote(url, opts);
	t.equal(remote.getName(), dummy_name, 'Check passing in the name option');
	t.equal(aHostnameOverride, remote._options['grpc.ssl_target_name_override'], 'GRPC Options tests: new Remote grpc with opts ssl-target-name-override created');
	t.ok(remote._endpoint.creds, 'GRPC Options tests: new Remote grpc with opts _endpoint.creds created');
	t.equal('some_value', remote._options['grpc.dummy_property'], 'GRPC options tests: pass-through option properties');
	t.equal(remote.getUrl(), url, 'checking that getURL works');
	t.equal(remote._grpc_wait_for_ready_timeout, 500, 'Remote should have grpc waitForReady timeout equals 500');

	t.comment('\n * PEER *');
	// Peer: secure grpcs, requires opts.pem
	opts = {pem: aPem};
	let peer = null;
	t.doesNotThrow(
		() => {
			peer = new Peer(url, opts);
		},
		'Check not passing any GRPC options.'
	);
github hyperledger / fabric-chaincode-node / test / unit / handler.js View on Github external
'pem': 'dummyPEMString',
			'ssl-target-name-override': 'dummyHost',
			'request-timeout': 12345,
			'another-property': 'dummyValue7',
			'key':'dummyKeyString',
			'cert':'dummyCertString',
			'grpc.max_send_message_length': 1,
			'grpc.max_receive_message_length': 2,
			'grpc.keepalive_time_ms': 3,
			'grpc.http2.min_time_between_pings_ms': 5,
			'grpc.keepalive_timeout_ms': 8,
			'grpc.http2.max_pings_without_data': 13,
			'grpc.keepalive_permit_without_calls': 21
		});

	t.equal(handler._options['grpc.ssl_target_name_override'], 'dummyHost', 'Test converting opts.ssl-target-name-override to grpc.ssl_target_name_override');
	t.equal(handler._options['grpc.default_authority'], 'dummyHost', 'Test converting opts.ssl-target-name-override to grpc.default_authority');
	t.equal(handler._options['request-timeout'], 12345, 'Test processing request-time option');
	t.equal(handler._options['another-property'], 'dummyValue7', 'Test processing another-property option');
	t.equal(handler._options['grpc.max_send_message_length'], 1, 'Test grpc.max_send_message_length set to 1');
	t.equal(handler._options['grpc.max_receive_message_length'], 2, 'Test grpc.max_receive_message_length set to 2');
	t.equal(handler._options['grpc.keepalive_time_ms'], 3, 'Test grpc.keepalive_time_ms set to 3');
	t.equal(handler._options['grpc.http2.min_time_between_pings_ms'], 5, 'Test grpc.http2.min_time_between_pings_ms set to 5');
	t.equal(handler._options['grpc.keepalive_timeout_ms'], 8, 'Test grpc.keepalive_timeout_ms set to 8');
	t.equal(handler._options['grpc.http2.max_pings_without_data'], 13, 'Test grpc.http2.max_pings_without_data set to 13');
	t.equal(handler._options['grpc.keepalive_permit_without_calls'], 21, 'Test grpc.keepalive_permit_without_calls set to 21');

	// The DNS can be case sensitive in some cases (eg Docker DNS)
	handler = new Handler(chaincodeObj, 'grpc://Peer.Example.com:7051');
	t.equal(handler._endpoint.addr, 'Peer.Example.com:7051', 'Test handler.addr value preserves casing');

	t.end();
github grpc / grpc-node / test / interop / interop_client.js View on Github external
function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
  // TODO(mlumish): enable TLS functionality
  var options = {};
  var creds;
  if (tls) {
    var ca_path;
    if (test_ca) {
      ca_path = path.join(__dirname, '../data/ca.pem');
      var ca_data = fs.readFileSync(ca_path);
      creds = grpc.credentials.createSsl(ca_data);
    } else {
      creds = grpc.credentials.createSsl();
    }
    if (host_override) {
      options['grpc.ssl_target_name_override'] = host_override;
      options['grpc.default_authority'] = host_override;
    }
  } else {
    creds = grpc.credentials.createInsecure();
  }
  var test = test_cases[test_case];

  var execute = function(err, creds) {
    assert.ifError(err);
    var client = new test.Client(address, creds, options);
    test.run(client, done, extra);
  };

  if (test.getCreds) {
    test.getCreds(extra.oauth_scope, function(err, new_creds) {
      assert.ifError(err);
github grpc / grpc-node / test / interop / interop_client.js View on Github external
function runTest(address, host_override, test_case, tls, test_ca, done, extra) {
  // TODO(mlumish): enable TLS functionality
  var options = {};
  var creds;
  if (tls) {
    var ca_path;
    if (test_ca) {
      ca_path = path.join(__dirname, '../data/ca.pem');
      var ca_data = fs.readFileSync(ca_path);
      creds = grpc.credentials.createSsl(ca_data);
    } else {
      creds = grpc.credentials.createSsl();
    }
    if (host_override) {
      options['grpc.ssl_target_name_override'] = host_override;
      options['grpc.default_authority'] = host_override;
    }
  } else {
    creds = grpc.credentials.createInsecure();
  }
  var test = test_cases[test_case];

  var execute = function(err, creds) {
    assert.ifError(err);
    var client = new test.Client(address, creds, options);
    test.run(client, done, extra);
  };

  if (test.getCreds) {
    test.getCreds(extra.oauth_scope, function(err, new_creds) {
      assert.ifError(err);