Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 41 additions & 0 deletions examples/health_check/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Health Check

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.

## Overview

The example uses the `grpc-health-check` package to:
- **Server**: Initialize a `HealthImplementation` and toggle the health status every 3 seconds.
- **Client**: Connect to the server and `watch` the health service for streamed status updates.

## Start the server

Run the server, which serves only the health checking API:

```
node server.js
```

## Run the client

In another terminal, run the client which watches the server for health check variations:

```
node client.js
```

## Expected Output

The client will begin to continuously receive state changes from the server:

**Server Output:**
```
Server running at http://0.0.0.0:50051
```

**Client Output:**
```
Health check status: SERVING
Health check status: NOT_SERVING
Health check status: SERVING
```
51 changes: 51 additions & 0 deletions examples/health_check/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* Copyright 2026 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 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.
*
*/

const grpc = require('@grpc/grpc-js');
const protoLoader = require('@grpc/proto-loader');
const { protoPath: HEALTH_PROTO_PATH } = require('grpc-health-check');

const packageDefinition = protoLoader.loadSync(
HEALTH_PROTO_PATH,
{keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true
});
const healthProto = grpc.loadPackageDefinition(packageDefinition).grpc.health.v1;

function main() {
const healthClient = new healthProto.Health('localhost:50051', grpc.credentials.createInsecure());

const watchStream = healthClient.watch({ service: '' });

watchStream.on('data', function (response) {
console.log(`Health check status: ${response.status}`);
});

watchStream.on('error', function (error) {
console.error('Health check error:', error.message);
});

watchStream.on('end', function () {
console.log('Health check stream ended.');
});
}

main();
47 changes: 47 additions & 0 deletions examples/health_check/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* Copyright 2026 gRPC authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* 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.
*
*/

const grpc = require('@grpc/grpc-js');
const { HealthImplementation } = require('grpc-health-check');

/**
* Starts an RPC server that toggles health checks status.
*/
function main() {
const server = new grpc.Server();

// Set up the health check service
const statusMap = {
'': 'SERVING',
};
const healthImpl = new HealthImplementation(statusMap);
healthImpl.addToServer(server);

// Toggle the health status continuously
let nextStatus = 'NOT_SERVING';
setInterval(() => {
healthImpl.setStatus('', nextStatus);
nextStatus = nextStatus === 'SERVING' ? 'NOT_SERVING' : 'SERVING';
}, 3000);

server.bindAsync('0.0.0.0:50051', grpc.ServerCredentials.createInsecure(), () => {
console.log('Server running at http://0.0.0.0:50051');
});
}

main();
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@grpc/grpc-js": "^1.10.2",
"@grpc/grpc-js-xds": "^1.10.0",
"@grpc/reflection": "^1.0.0",
"grpc-health-check": "^2.1.0",
"@q42philips/node-grpc-error-details": "^2.1.0",
"lodash": "^4.6.1",
"minimist": "^1.2.0"
Expand Down
Loading