Ping
Send a health-check ping with the KubeMQ Go SDK to verify server connectivity and read server information.
Overview
A ping is a lightweight liveness check — you call it to confirm the broker is actually reachable before sending real traffic, without standing up a publisher, subscriber, or queue client just to find out. It's the tool of choice for startup readiness checks, Kubernetes liveness/readiness probes, and connection-health dashboards that need a fast, cheap go/no-go signal.
client.Ping(ctx) issues a minimal gRPC health-check RPC to the server and returns a *ServerInfo (host, version, uptime) confirming the broker answered. It works over the same connection regardless of which messaging pattern you use elsewhere on that client — events, queues, commands, or queries — and doesn't touch any channel.
Gotchas: a failed Ping doesn't close the client — the SDK's reconnect logic keeps retrying in the background, so check the returned error yourself rather than assume the client tears itself down. A successful ping only confirms the broker process answered, not that a specific channel or queue exists or has capacity. And since the gRPC channel is often established lazily, the first call you make is what actually triggers the connection, so a ping right after NewClient can still surface setup errors.
Prerequisites
- KubeMQ server running on
localhost:50000 - Go SDK installed (
go get github.com/kubemq-io/kubemq-go/v2)
Code
// Example: connection/ping
//
// Demonstrates how to ping a KubeMQ server to verify connectivity and
// retrieve server information (host, version, uptime).
//
// Channel: go-connection.ping
// Client ID: go-connection-ping-client
//
// Run with a KubeMQ server on localhost:50000
// (see https://docs.kubemq.io/deploy).
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/kubemq-io/kubemq-go/v2"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
client, err := kubemq.NewClient(ctx,
kubemq.WithAddress("localhost", 50000),
kubemq.WithClientId("go-connection-ping-client"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Ping the server to verify connectivity and retrieve server info.
info, err := client.Ping(ctx)
if err != nil {
log.Fatalf("Ping failed: %v", err)
}
fmt.Printf("Ping OK: host=%s version=%s uptime=%ds\n",
info.Host, info.Version, info.ServerUpTimeSeconds)
}
How It Works
client.Ping(ctx)sends a lightweight gRPC health-check RPC to the broker and returns*ServerInfoon success.info.Hostis the broker's advertised hostname,info.Versionis its build version string, andinfo.ServerUpTimeSecondsis how long the server has been running — useful for dashboards and startup readiness checks.- A
Pingerror does not close the client; the gRPC reconnect policy continues to attempt reconnection. UsePingin readiness probes that need to surface temporary unavailability. - The 10-second context deadline bounds both the
NewClientdial and thePingcall.
Related
Was this page helpful?