Health Check server and client for nodejs applications using ipc via unix and windows sockets.
NodeJS version >=14 is required for this module.
Install via npm:
npm install ipc-healthcheck
Include the server
const HealthcheckServer = require('ipc-healthcheck/healthcheck-server');
Creating a new server object Parameter:
namespace
: String - Health check server name clients can connect to.respondTime
: int - Time in ms the server gives the client to respondintervalTime
: int - Time in ms the server waits between every requestsilent
: boolean - Turns debug messages on or off. true = no debug messages
const server = new HealthcheckServer(namespace, respondTime, intervalTime, silent);
The server has the following methods:
Start the socket server and starts sending requests to all connected clients. (void)
server.startServer()
Stops sending requests and stops the server. (void)
server.stopServer()
The server does fire the following events:
- serviceRegistered - A new service has registered with the server
- serviceCrashed - A service didn't answer the health check request
- serviceNotify - A service send a message to the server
- serviceDetached - A new service has registered with the server
A new client connected and registered to the Server. Healthcheck messages are now sent to this client.
Parameters
service
: Object - Service objectname
: String - Name with which the service is registeredid
: String - ID the Server has given this service
server.on('serviceRegistered', (service) => {
//do stuff
});
A client that registered at the Server did not answer the last 3 health check requests and might be crashed.
Parameters:
service
: Object - Service objectname
: String - Name with which the service is registeredid
: String - ID the Server has given this servicelastrespond
: Date - Time the service did the last respond to a request.
server.on('serviceCrashed', (service) => {
//do suff
});
This event triggers, when a client sends an message to the server.
Parameters:
msg
: String - Message the client send to the Serverservice
: Object - Service objectname
: String - Name with which the service is registeredid
: String - ID the Server has given this servicelastrespond
: Date - Time the service did the last respond to a request.
server.on('serviceNotify', (msg, service) => {
//do stuff
});
A client that was connected to the server called the detach function and disconnected. This client will not receive any healthcheck messages.
Parameters:
service
: Object - Service objectname
: String - Name with which the service is registeredid
: String - ID the Server has given this servicelastrespond
: Date - Time the service did the last respond to a request.
server.on('serviceDetached', (service) => {
//do stuff
});
Include the client
const HealthcheckClient = require('ipc-healthcheck/healthcheck-client');
Creating a new client object
Parameter:
namespace
: String - Health check server name the client should connect toserviceID
: _String - The name with which the client registers with the serversilent
: boolean - Turns debug messages on or off. true = no debug messages
const healthcheckclient = new HealthcheckClient(namespace,serviceID,silent);
The client has the following methods:
Client connect to the server and starts listening to health check requests. (void)
client.startListening();
Sends a message to the server (void)
Parameters:
msg
: String - The message that should be send to the server
client.notify(msg);
Tell the server to not get any requests anymore and disconnect. (void)
client.detach();
You can find examples in this repo.