Skip to content

Commit 22ef6a1

Browse files
chore(grpc): migrate from grpc to @grpc/grpc-js in getting-started-grpc example (#4077)
* chore(deps): replace grpc with @grpc/grpc-js and @grpc/proto-loader * refactor(server): migrate grpc server to @grpc/grpc-js and proto-loader * refactor(client): migrate grpc client to @grpc/grpc-js and proto-loader * chore(server): remove deprecated server.start() call after bindAsync * docs(readme): improve client.js usage docs with JWT and API examples * docs(readme): update get-id-token README.md reference
1 parent d3e50be commit 22ef6a1

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

endpoints/getting-started-grpc/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,29 @@ $ node server.js -p 50051
1313
```
1414

1515
### Run the client
16+
For running the client locally, you'll need either an API key or a JWT auth token:
17+
18+
#### Using a JWT token (recommended for local development)
19+
You can generate a development-only JWT token using Google Cloud CLI:
20+
21+
```
22+
$ gcloud auth print-identity-token
23+
```
24+
See [get-id-token#generic-dev](https://cloud.google.com/docs/authentication/get-id-token#generic-dev) for more info.
25+
26+
Then run the client using this token:
1627
```
17-
$ node client.js -h localhost:50051
28+
$ node client.js -h localhost:50051 -j YOUR_JWT_TOKEN
1829
```
1930

31+
#### Using an API key
32+
Alternatively, you can use an API key from your Google Cloud project:
33+
```
34+
$ node client.js -h localhost:50051 -k YOUR_API_KEY
35+
```
36+
37+
You can create API keys in the [Google Cloud Console](https://console.cloud.google.com/apis/credentials).
38+
2039
## Running on Google Cloud Platform
2140
### Setup
2241
Make sure you have [gcloud][gcloud] and [Node.js][nodejs] installed.

endpoints/getting-started-grpc/client.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ const makeGrpcRequest = (JWT_AUTH_TOKEN, API_KEY, HOST, GREETEE) => {
2323
// const GREETEE = 'world';
2424

2525
// Import required libraries
26-
const grpc = require('grpc');
26+
const grpc = require('@grpc/grpc-js');
27+
const protoLoader = require('@grpc/proto-loader');
2728
const path = require('path');
2829

2930
// Load protobuf spec for an example API
3031
const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto');
31-
const protoObj = grpc.load(PROTO_PATH).helloworld;
32+
33+
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
34+
keepCase: true,
35+
longs: String,
36+
enums: String,
37+
defaults: true,
38+
oneofs: true,
39+
});
40+
41+
const protoObj = grpc.loadPackageDefinition(packageDefinition).helloworld;
3242

3343
// Create a client for the protobuf spec
3444
const client = new protoObj.Greeter(HOST, grpc.credentials.createInsecure());

endpoints/getting-started-grpc/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"test": "mocha --exit system-test/*.test.js --timeout=60000"
1818
},
1919
"dependencies": {
20-
"grpc": "^1.18.0",
20+
"@grpc/grpc-js": "^1.13.3",
21+
"@grpc/proto-loader": "^0.7.15",
2122
"yargs": "^17.0.0"
2223
},
2324
"devDependencies": {
@@ -26,4 +27,3 @@
2627
"wait-port": "^1.0.4"
2728
}
2829
}
29-

endpoints/getting-started-grpc/server.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,17 @@
1717
const path = require('path');
1818
const PROTO_PATH = path.join(__dirname, '/protos/helloworld.proto');
1919

20-
const grpc = require('grpc');
21-
const helloProto = grpc.load(PROTO_PATH).helloworld;
20+
const grpc = require('@grpc/grpc-js');
21+
const protoLoader = require('@grpc/proto-loader');
22+
23+
const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
24+
keepCase: true,
25+
longs: String,
26+
enums: String,
27+
defaults: true,
28+
oneofs: true,
29+
});
30+
const helloProto = grpc.loadPackageDefinition(packageDefinition).helloworld;
2231

2332
// Implement the SayHello RPC method.
2433
const sayHello = (call, callback) => {
@@ -28,9 +37,14 @@ const sayHello = (call, callback) => {
2837
// Start an RPC server to handle Greeter service requests
2938
const startServer = PORT => {
3039
const server = new grpc.Server();
31-
server.addProtoService(helloProto.Greeter.service, {sayHello: sayHello});
32-
server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure());
33-
server.start();
40+
server.addService(helloProto.Greeter.service, {sayHello: sayHello});
41+
server.bindAsync(
42+
`0.0.0.0:${PORT}`,
43+
grpc.ServerCredentials.createInsecure(),
44+
() => {
45+
console.log(`gRPC server started on port ${PORT}`);
46+
}
47+
);
3448
};
3549

3650
// The command-line program

0 commit comments

Comments
 (0)