List Channels
List all channels on the KubeMQ server with their metadata and statistics using the Go SDK management API.
Overview
Listing channels turns the broker into a discoverable inventory instead of a black box — instead of hardcoding channel names everywhere, you ask the server what actually exists right now. That's exactly what monitoring dashboards, cleanup scripts, and "did my deployment create the channels it should have" checks need. It's read-only and has no effect on message flow, so it's safe to run against production at any time.
Under the hood, client.ListChannels(ctx, channelType, searchQuery) queries channels of one type — events, events-store, queues, commands, or queries — and an optional searchQuery narrows results server-side by substring instead of filtering client-side. Typed helpers like ListEventsChannels and ListQueuesChannels wrap the same call without the ChannelType constant. Each result includes the name, an IsActive flag, and traffic statistics.
Gotchas: the search filter is a substring match, not a glob or regex. IsActive and the stat counters are a snapshot at query time, so a channel can go idle a moment later. And no matches returns an empty slice, not an error — check length rather than an exception path.
Prerequisites
- KubeMQ server running on
localhost:50000 - Go SDK installed (
go get github.com/kubemq-io/kubemq-go/v2)
Code
// Example: management/list-channels
//
// Demonstrates listing channels with optional search filter.
// You can list all channels of a type or filter by name prefix.
//
// Channel: go-management.list-channels
// Client ID: go-management-list-channels-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-list-channels-client"),
)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// List all events channels.
channels, err := client.ListChannels(ctx, kubemq.ChannelTypeEvents, "")
if err != nil {
log.Println("ListChannels:", err)
} else {
fmt.Printf("All events channels: %d found\n", len(channels))
for _, ch := range channels {
fmt.Printf(" - %s (active=%v)\n", ch.Name, ch.IsActive)
}
}
// List channels with a search filter.
filtered, err := client.ListChannels(ctx, kubemq.ChannelTypeQueues, "go-")
if err != nil {
log.Println("ListChannels filtered:", err)
} else {
fmt.Printf("Queue channels matching 'go-': %d found\n", len(filtered))
for _, ch := range filtered {
fmt.Printf(" - %s\n", ch.Name)
}
}
// Typed convenience methods — no channel-type constant needed.
eventsChannels, err := client.ListEventsChannels(ctx, "")
if err != nil {
log.Println("ListEventsChannels:", err)
} else {
fmt.Printf("Events channels (typed): %d found\n", len(eventsChannels))
for _, ch := range eventsChannels {
fmt.Printf(" - %s (active=%v)\n", ch.Name, ch.IsActive)
}
}
queuesChannels, err := client.ListQueuesChannels(ctx, "go-")
if err != nil {
log.Println("ListQueuesChannels:", err)
} else {
fmt.Printf("Queue channels (typed, matching 'go-'): %d found\n", len(queuesChannels))
for _, ch := range queuesChannels {
fmt.Printf(" - %s\n", ch.Name)
}
}
}
How It Works
client.ListChannels(ctx, channelType, searchQuery)returns a[]*ChannelInfoslice; an emptysearchQuerystring returns all channels of that type.- Passing
"go-"as the search query filters to channels whose names contain that prefix, reducing the result set on servers with many channels. - Each
ChannelInfoitem includesName,IsActive(has active subscribers), and additional statistics fields like message counts and last activity timestamps. - The typed helpers (
ListEventsChannels,ListQueuesChannels, etc.) remove the need for theChannelTypeconstant and are the recommended API for type-safe code.
Related
Was this page helpful?