Skip to content

Commit 2670b37

Browse files
authored
Merge pull request #3046 from zarinn3pal/feat/health_check_example
feat: Added health check example
2 parents 37af6c0 + fd67ca6 commit 2670b37

4 files changed

Lines changed: 140 additions & 0 deletions

File tree

examples/health_check/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Health Check
2+
3+
This example demonstrates how to set up standard gRPC Health Checks utilizing the unified [grpc-health-check](https://www.npmjs.com/package/grpc-health-check) library.
4+
5+
## Overview
6+
7+
The example uses the `grpc-health-check` package to:
8+
- **Server**: Initialize a `HealthImplementation` and toggle the health status every 3 seconds.
9+
- **Client**: Connect to the server and `watch` the health service for streamed status updates.
10+
11+
## Start the server
12+
13+
Run the server, which serves only the health checking API:
14+
15+
```
16+
node server.js
17+
```
18+
19+
## Run the client
20+
21+
In another terminal, run the client which watches the server for health check variations:
22+
23+
```
24+
node client.js
25+
```
26+
27+
## Expected Output
28+
29+
The client will begin to continuously receive state changes from the server:
30+
31+
**Server Output:**
32+
```
33+
Server running at http://0.0.0.0:50051
34+
```
35+
36+
**Client Output:**
37+
```
38+
Health check status: SERVING
39+
Health check status: NOT_SERVING
40+
Health check status: SERVING
41+
```

examples/health_check/client.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
*
3+
* Copyright 2026 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
const grpc = require('@grpc/grpc-js');
20+
const protoLoader = require('@grpc/proto-loader');
21+
const { protoPath: HEALTH_PROTO_PATH } = require('grpc-health-check');
22+
23+
const packageDefinition = protoLoader.loadSync(
24+
HEALTH_PROTO_PATH,
25+
{keepCase: true,
26+
longs: String,
27+
enums: String,
28+
defaults: true,
29+
oneofs: true
30+
});
31+
const healthProto = grpc.loadPackageDefinition(packageDefinition).grpc.health.v1;
32+
33+
function main() {
34+
const healthClient = new healthProto.Health('localhost:50051', grpc.credentials.createInsecure());
35+
36+
const watchStream = healthClient.watch({ service: '' });
37+
38+
watchStream.on('data', function (response) {
39+
console.log(`Health check status: ${response.status}`);
40+
});
41+
42+
watchStream.on('error', function (error) {
43+
console.error('Health check error:', error.message);
44+
});
45+
46+
watchStream.on('end', function () {
47+
console.log('Health check stream ended.');
48+
});
49+
}
50+
51+
main();

examples/health_check/server.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
*
3+
* Copyright 2026 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
const grpc = require('@grpc/grpc-js');
20+
const { HealthImplementation } = require('grpc-health-check');
21+
22+
/**
23+
* Starts an RPC server that toggles health checks status.
24+
*/
25+
function main() {
26+
const server = new grpc.Server();
27+
28+
// Set up the health check service
29+
const statusMap = {
30+
'': 'SERVING',
31+
};
32+
const healthImpl = new HealthImplementation(statusMap);
33+
healthImpl.addToServer(server);
34+
35+
// Toggle the health status continuously
36+
let nextStatus = 'NOT_SERVING';
37+
setInterval(() => {
38+
healthImpl.setStatus('', nextStatus);
39+
nextStatus = nextStatus === 'SERVING' ? 'NOT_SERVING' : 'SERVING';
40+
}, 3000);
41+
42+
server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
43+
console.log('Server running at http://0.0.0.0:50051');
44+
});
45+
}
46+
47+
main();

examples/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@grpc/grpc-js": "^1.10.2",
99
"@grpc/grpc-js-xds": "^1.10.0",
1010
"@grpc/reflection": "^1.0.0",
11+
"grpc-health-check": "^2.1.0",
1112
"@q42philips/node-grpc-error-details": "^2.1.0",
1213
"lodash": "^4.6.1",
1314
"minimist": "^1.2.0"

0 commit comments

Comments
 (0)