|
| 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(); |
0 commit comments