O'Reilly logo

TypeScript Microservices by Parth Ghiya

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending heartbeats and doing a health check

Now, we will add one scheduler that constantly sends heartbeats to tell our Consul server that it is active. In the same code as the previous exercise, just add the following lines of code:

  setInterval(() => {   consul.agent.check.pass({id:`service:${CONSUL_ID}`},    (err:any) => {        if (err) throw new Error(err);         console.log('Send out heartbeat to consul');        });   }, 5 * 1000);

What did we do?

  • Every five seconds, we send out heartbeats to Consul, to ensure that our service with the CONSUL_ID that we generated is active.
  • Periodic heartbeats are sent out to make sure that Consul knows that our service is active and it doesn't disconnect our service. Earlier, we kept TTL values in our settings as 10 seconds, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required