KubeMQ
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

main.go
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

send_event.go
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

receive_events.go
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

OptionDefaultDescription
WithAddress(host, port)localhost:50000KubeMQ server address
WithClientId(id)Auto-generated UUIDUnique client identifier
WithCredentialProvider(p)NoneAuthentication credential provider
WithTLS(caCertFile)None (plaintext)TLS with CA cert file
WithReconnectPolicy(p)Infinite retries, 1-30s backoffReconnection behavior
WithConnectionTimeout(d)10sInitial connection timeout
WithRetryPolicy(p)3 retries, 100ms-10s backoffRetry for transient failures
WithLogger(l)NoneStructured logger
WithTracerProvider(tp)NoneOpenTelemetry tracer
WithMeterProvider(mp)NoneOpenTelemetry 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

Was this page helpful?

On this page