Client SDKsGoTutorials
Send Your First Message
Connect the Go client to KubeMQ and publish and receive your first message end to end.
This is your first hands-on lesson with the Go SDK: create a client, send an event, and receive it. Make sure you have the SDK installed (see the Go SDK overview).
Create a Client
package main
import (
"context"
"log"
"github.com/kubemq-io/kubemq-go/v2"
)
func main() {
ctx := context.Background()
client, err := kubemq.NewClient(ctx,
kubemq.WithAddress("localhost", 50000),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
log.Println("Connected to KubeMQ")
}Send Your First Event
err = client.SendEvent(ctx, kubemq.NewEvent().
SetChannel("notifications").
SetBody([]byte("hello kubemq")),
)
if err != nil {
log.Fatal(err)
}
log.Println("Event sent successfully")Receive Events
sub, err := client.SubscribeToEvents(ctx, "notifications", "",
kubemq.WithOnEvent(func(event *kubemq.Event) {
fmt.Printf("Received: %s\n", event.Body)
}),
kubemq.WithOnError(func(err error) {
log.Println("Error:", err)
}),
)
if err != nil {
log.Fatal(err)
}
defer sub.Unsubscribe()Configuration Options
| Option | Default | Description |
|---|---|---|
WithAddress(host, port) | localhost:50000 | KubeMQ server address |
WithClientId(id) | Auto-generated UUID | Unique client identifier |
WithCredentialProvider(p) | None | Authentication credential provider |
WithTLS(caCertFile) | None (plaintext) | TLS with CA cert file |
WithReconnectPolicy(p) | Infinite retries, 1-30s backoff | Reconnection behavior |
WithConnectionTimeout(d) | 10s | Initial connection timeout |
WithRetryPolicy(p) | 3 retries, 100ms-10s backoff | Retry for transient failures |
WithLogger(l) | None | Structured logger |
WithTracerProvider(tp) | None | OpenTelemetry tracer |
WithMeterProvider(mp) | None | OpenTelemetry meter |
Error Handling
All SDK operations return errors as *kubemq.KubeMQError with structured error information:
err := client.SendEvent(ctx, event)
if err != nil {
var ke *kubemq.KubeMQError
if errors.As(err, &ke) {
fmt.Printf("Code: %s, Retryable: %v\n", ke.Code, ke.IsRetryable)
switch ke.Code {
case kubemq.ErrCodeTimeout:
// Retry with longer timeout
case kubemq.ErrCodeAuthentication:
// Check credentials
case kubemq.ErrCodeTransient:
// Automatic retry exhausted
}
}
}Next Steps
- Go SDK Reference — full API documentation
- Go SDK Examples — complete examples for all patterns
- GitHub Repository — source code and issues
Was this page helpful?