Create Channel
Create a KubeMQ messaging channel programmatically using the Go SDK management API.
Overview
KubeMQ auto-creates a channel the first time a client publishes or subscribes to it — convenient for prototyping, but a liability once channels are infrastructure you need to reason about. Pre-creating channels with the management API lets you provision topology before any producer or consumer connects: enforce naming conventions in a startup script, stand up the channels a service depends on as part of deployment, or fail fast if a required channel is missing instead of it silently springing into existence.
client.CreateChannel(ctx, name, channelType) registers a channel of a given kubemq.ChannelType (ChannelTypeEvents, ChannelTypeQueues, ChannelTypeEventsStore, etc.) with the server. Typed wrappers like CreateEventsChannel and CreateQueuesChannel do the same without a type constant.
Gotchas: the call is idempotent for a matching name and type, so it's safe to run on every startup — but a channel's type is fixed at creation, and reusing the name with a different type fails rather than migrating it. Creation only registers the channel; it does not start a consumer, so a freshly created queue or events channel happily accepts messages with nothing yet reading them.
Prerequisites
- KubeMQ server running on
localhost:50000 - Go SDK installed (
go get github.com/kubemq-io/kubemq-go/v2)
Code
// Example: management/create-channel
//
// Demonstrates creating channels of different types (events, queues, etc.).
// Channels can be pre-created for organizational purposes.
//
// Channel: go-management.create-channel
// Client ID: go-management-create-channel-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(), 30*time.Second)
defer cancel()
client, err := kubemq.NewClient(ctx,
kubemq.WithAddress("localhost", 50000),
kubemq.WithClientId("go-management-create-channel-client"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// Create an events channel.
err = client.CreateChannel(ctx, "go-management.create-channel.events", kubemq.ChannelTypeEvents)
if err != nil {
log.Println("CreateChannel events:", err)
} else {
fmt.Println("Created events channel: go-management.create-channel.events")
}
// Create a queues channel.
err = client.CreateChannel(ctx, "go-management.create-channel.queues", kubemq.ChannelTypeQueues)
if err != nil {
log.Println("CreateChannel queues:", err)
} else {
fmt.Println("Created queue channel: go-management.create-channel.queues")
}
// Create an events store channel.
err = client.CreateChannel(ctx, "go-management.create-channel.es", kubemq.ChannelTypeEventsStore)
if err != nil {
log.Println("CreateChannel events_store:", err)
} else {
fmt.Println("Created events store channel: go-management.create-channel.es")
}
// Typed convenience methods — no channel-type constant needed.
err = client.CreateEventsChannel(ctx, "go-management.typed.events")
if err != nil {
log.Println("CreateEventsChannel:", err)
} else {
fmt.Println("Created events channel (typed): go-management.typed.events")
}
err = client.CreateQueuesChannel(ctx, "go-management.typed.queues")
if err != nil {
log.Println("CreateQueuesChannel:", err)
} else {
fmt.Println("Created queues channel (typed): go-management.typed.queues")
}
err = client.CreateCommandsChannel(ctx, "go-management.typed.commands")
if err != nil {
log.Println("CreateCommandsChannel:", err)
} else {
fmt.Println("Created commands channel (typed): go-management.typed.commands")
}
}
How It Works
client.CreateChannel(ctx, name, channelType)is the generic form that accepts akubemq.ChannelTypeconstant (ChannelTypeEvents,ChannelTypeQueues,ChannelTypeEventsStore, etc.).- The typed convenience methods (
CreateEventsChannel,CreateQueuesChannel,CreateCommandsChannel) call the same underlying API without requiring a type constant — useful when the type is known at compile time. - Channel pre-creation is optional; KubeMQ auto-creates channels on first use. Pre-creating is useful for enforcing naming standards, applying retention policies, or setting up infrastructure before applications connect.
- Errors from
CreateChannelare non-fatal if the channel already exists; the SDK returns a descriptive error that can be inspected withvar ke *kubemq.KubeMQError; errors.As(err, &ke).
Related
Was this page helpful?