Skip to content

chore(grpc): migrate from grpc to @grpc/grpc-js in getting-started-grpc example #4077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
21 changes: 20 additions & 1 deletion endpoints/getting-started-grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,29 @@ $ node server.js -p 50051
```

### Run the client
For running the client locally, you'll need either an API key or a JWT auth token:

#### Using a JWT token (recommended for local development)
You can generate a development-only JWT token using Google Cloud CLI:

```
$ gcloud auth print-identity-token
```
See [get-id-token#generic-dev](https://cloud.google.com/docs/authentication/get-id-token#generic-dev) for more info.

Then run the client using this token:
```
$ node client.js -h localhost:50051
$ node client.js -h localhost:50051 -j YOUR_JWT_TOKEN
```

#### Using an API key
Alternatively, you can use an API key from your Google Cloud project:
```
$ node client.js -h localhost:50051 -k YOUR_API_KEY
```

You can create API keys in the [Google Cloud Console](https://console.cloud.google.com/apis/credentials).

## Running on Google Cloud Platform
### Setup
Make sure you have [gcloud][gcloud] and [Node.js][nodejs] installed.
Expand Down
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(),
() => {
console.log(`gRPC server started on port ${PORT}`);
}
);
};

// The command-line program
Expand Down
Loading