Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions endpoints/getting-started-grpc/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ const makeGrpcRequest = (JWT_AUTH_TOKEN, API_KEY, HOST, GREETEE) => {
// const GREETEE = 'world';

// Import required libraries
const grpc = require('grpc');
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const path = require('path');

// Load protobuf spec for an example API
const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto');
const protoObj = grpc.load(PROTO_PATH).helloworld;

const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});

const protoObj = grpc.loadPackageDefinition(packageDefinition).helloworld;

// Create a client for the protobuf spec
const client = new protoObj.Greeter(HOST, grpc.credentials.createInsecure());
Expand Down
4 changes: 2 additions & 2 deletions endpoints/getting-started-grpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"test": "mocha --exit system-test/*.test.js --timeout=60000"
},
"dependencies": {
"grpc": "^1.18.0",
"@grpc/grpc-js": "^1.13.3",
"@grpc/proto-loader": "^0.7.15",
"yargs": "^17.0.0"
},
"devDependencies": {
Expand All @@ -26,4 +27,3 @@
"wait-port": "^1.0.4"
}
}

24 changes: 19 additions & 5 deletions endpoints/getting-started-grpc/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@
const path = require('path');
const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto');

const grpc = require('grpc');
const helloProto = grpc.load(PROTO_PATH).helloworld;
const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');

const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;

// Implement the SayHello RPC method.
const sayHello = (call, callback) => {
Expand All @@ -28,9 +37,14 @@ const sayHello = (call, callback) => {
// Start an RPC server to handle Greeter service requests
const startServer = PORT => {
const server = new grpc.Server();
server.addProtoService(helloProto.Greeter.service, {sayHello: sayHello});
server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure());
server.start();
server.addService(helloProto.Greeter.service, {sayHello: sayHello});
server.bindAsync(
`0.0.0.0:${PORT}`,
grpc.ServerCredentials.createInsecure(),
() => {
server.start();
}
);
};

// The command-line program
Expand Down
Loading