Get Sync Gateway heartbeat

I have few clients each with installed Couchbase Lite and Sync Gateway and every client’s database is replicated to a different bucket on a server.

I know that from the client I can check the heartbeat of the sync using:
curl -X GET ‘localhost:4984/db/_changes?feed=continuous&heartbeat=26000&since=0’

How do I do that from the server? What is the proper way of doing so?

Basically, I need to know if one of the Sync Gateway instances is offline and which without knowing their IPs.

Can you answer @borrrden ?

On the server, I have a service(REST API) and in the SG JSON config I have an event handler that sends updates to it:

“event_handlers”: {
“document_changed”: [
“handler”: “webhook”,
“url”: “http://serverip:port/events/newupdate?=clientId”

Can I somehow add a custom event for the heartbeat?

Can you explain a little more of what you’re trying to do?

The typical way to handle a single Sync Gateway node failing is to front the Sync Gateway tier with a load balancer. In that case the load balancer will need to know the IP addresses / hostnames of the SG nodes.

Does “server” refer to your web/application server?

It sounds like you might be trying to figure out how to get Sync Gateway to send outbound heartbeats via webhooks for the purpose of monitoring. It feels like a very Rube Goldberg approach to monitoring, but if that’s your only option then you could configure webhooks, and then have a background process running that could generate webhook events, for example by posting updates to a “heartbeat” document.

Thank you for your answer!

Can you explain a little more of what you’re trying to do?

Yes, I want to use the continuous replication to monitor deployed system’s states. Each deployment right now consists of .NET Core application, Couchbase Lite, and Sync Gateway. Basically, I store the state changes in the local database(Lite), since the .NET application needs that data, and then synchronize them with a server(Couchbase server).

Moreover, I have another .NET application, connected to the server, that provides live monitoring of all of the system’s states. I managed to achieve all of that so far, but the problem is that the systems are not online all the time. Thus I want to know when their Sync Gateways are connected to the server, so I can either ensure that the latest updates are present or the system(client) is “disconnected”.

The systems are deployed on networks with non-static IP addresses, so using them is not an option.

I do apologize in advance if that is not the right approach of utilizing the benefits of the Couchbase products, but I just started using them.

Is the webhook + background process the right approach of achieving that or there is a better way?

Thank you in advance!

I think what @traun described is essentially creating your own “heartbeat” by updating a doc that you can then use to trigger a webhook. I don’t believe there’s a way to generate a webhook based on the changes feed heartbeat itself. Traun can correct me if I’m wrong, but I don’t think webhooks provide any real failure handling, so this may not be robust enough for what you want, either.

If I’m understanding your problem correctly, you want to know when a client has been online and synced up all data changes? And you’re saying SG isn’t always up and connected to CBS?

Regardless, for at least part of what I think you’re asking, my inclination would be to have a lightweight service sitting next to SG on the same server monitoring the changes feed. I’m not clear on whether your .NET monitoring service sits on a static IP. If it is, then mini-service could call out to that. (There’s another possibility, namely using Couchbase Server Eventing, which could eliminate the need for another app, but I think that route has other complications.) I’d also look at whether the log output can give you the information you want and consider monitoring that. That could be less heavy-weight than watching a full changes feed.

If neither SG, nor your monitoring service are on static IP, you’re going to have to figure out a discovery/coordination approach yourself first.

Rethinking a bit, something like Traun’s approach seems like a better idea. Update a dedicated “last sync completed timestamp” document. I don’t think I’d monitor this via SG, though, but through CBS itself. This does have a downside of having a window where the client has synced everything but the timestamp document before going offline. At which point your monitor would have the wrong status.

Thank you for the detailed response. I have achieved it using the approach that Traun suggested, creating a new “heartbeat” document and updating it. How can I monitor this through CBS? Should I use the Couchbase Eventing Service and its OnUpdate function to monitor the changes in the documents with “heartbeat” type or there is a better approach using the .NET SDK?